Java Constructor

Introduction

Java constructor or constructors in Java is a term use to describe how we build things in our programs. A constructor in Java is a special method that is use to initialize objects. The constructor is call when an object of a class is create. It may be use to initialize the values of object attributes.

In Java, a constructor is a block of codes similar to the method. It is called when an instance of the class is created. At the time of calling the constructor, memory for the object is allocate in the memory. It is a sort of method that is use to initialize an object. Every time an object is created using the new() keyword, at least one constructor is called.

Note: It is not necessary to write a constructor for a class. It is because the java compiler creates a default constructor if your class doesn’t have any.

Code 01: java constructor example

class Main {
  private String website;
  // constructor
  Main() {
    website = "'softhunt.net'";
  }

  public static void main(String[] args) {

    // constructor is invoked while
    // creating an object of the Main class
    Main obj = new Main();
    System.out.println("Welcome to " + obj.website);
  }
}

Output:

Welcome to 'softhunt.net'

When Java Constructor is called?

Each time an object is create using a new() keyword, at least one constructor (it could be the default constructor) is invoke to assign initial values to the data members of the same class.

Java Constructor Rules

  1. The constructor of a class must have the same name as the class name in which it resides.
  2. A constructor in Java can not be abstract, final, static, or Synchronized.
  3. Access modifiers can be use in constructor declaration to control its access i.e which other class can call the constructor.

Java Constructor Types

In Java, constructors divides into three types

  1. No-Arg Constructor
  2. Parameterized Constructor
  3. Default Constructor

Type 01: Java No-Arg Constructors

Similar to methods, Java constructors may or may not have any parameters (arguments). If a constructor does not accept any parameters, it is known as a no-argument constructor.

Code 02: Java private no-arg constructors

class Main {

  int i;

  // constructor with no parameter
  private Main() {
    i = 15;
    System.out.println("Constructor called \n");
  }

  public static void main(String[] args) {

    // calling the constructor without any parameter
    Main obj = new Main();
    System.out.println("Value of i: " + obj.i);
  }
}

Output:

Constructor called 

Value of i: 15

Note: Once a constructor is declare private, it cannot be access from outside the class. So, creating objects from outside the class is prohibite using the private constructor. However, if we want to create objects outside the class, then we need to declare the constructor as public.

Code 03: Java public no-arg constructors

class Tutorialweb {
  String website;

  // public constructor
  public Tutorialweb() {
    website = "Softhunt.net";
  }
}

class Main {
  public static void main(String[] args) {

    // object is created in another class
    Tutorialweb obj = new Tutorialweb();
    System.out.println("Tutorial website name = " + obj.website);
  }
}

Output:

Tutorial website name = Softhunt.net

Type 02: Parameterized Constructor

Java constructors can also accept one or more parameters. Such constructors are known as parameterized constructors (constructors with parameters).

Code 04:

class Main {

  String fruits;

  // constructor accepting single value
  Main(String fruit) {
    fruits = fruit;
    System.out.println("Fruit " + fruits);
  }

  public static void main(String[] args) {

    // call constructor by passing a single value
    Main obj1 = new Main("Banana");
    Main obj2 = new Main("Apple");
    Main obj3 = new Main("Mango");
  }
}

Output:

Fruit Banana
Fruit Apple
Fruit Mango

Type 03: Default Constructor

If we do not create any constructor. The compiler automatically creates a no-arg constructor during the execution of the program. This constructor is call as the default constructor.

Code 05:

class Main {

  int a;
  boolean b;
  byte c;
  short d;
  long e;
  char f;
  float g;

  public static void main(String[] args) {

    // A default constructor is called
    Main obj = new Main();

    System.out.println("Default Value:");
    System.out.println("int = " + obj.a);
    System.out.println("boolean = " + obj.b);
    System.out.println("byte = " + obj.c);
    System.out.println("short = " + obj.d);
    System.out.println("long = " + obj.e);
    System.out.println("char = " + obj.f);
    System.out.println("float = " + obj.g);
  }
}

Output:

Default Value:
int = 0
boolean = false
byte = 0
short = 0
long = 0
char = 
float = 0.0

Java Constructor Overloading

Similar to Java method overloading, we can also create two or more constructors with different parameters. This is call as constructor overloading.

Code 06:

class Main {

  String fruit;

  // constructor with no parameter
  Main() {
    this.fruit = "Banana";
  }

  // constructor with a single parameter
  Main(String fruit) {
    this.fruit = fruit;
  }

  public void getName() {
    System.out.println("Fruit: " + this.fruit);
  }

  public static void main(String[] args) {

    // call constructor with no parameter
    Main obj1 = new Main();

    // call constructor with a single parameter
    Main obj2 = new Main("Apple");

    obj1.getName();
    obj2.getName();
  }
}

Output:

Fruit: Banana
Fruit: Apple

Note: We have used the java constructor this keyword to specify the variable of the class.

Conclusion

That’s all for this article, if you have any confusion contact us through our website or email us at [email protected] or by using LinkedIn

Suggested Articles:

  1. How To Call a Method in Java – [With Examples]

Leave a Comment