Constructor in java

Let’s learn constructor in java.

Constructor in java

Constructor is a special method or block of code used to initialize an object. It executes automatically when you create an object.

constructor in java

Example:

class DemoClass
{
   // constructor name is same as class name
   DemoClass()
   {
      ....
   }
}

// calls DemoClass() constructor
DemoClass obj = new DemoClass();

In the above code when we type new DemoClass() that’s when actually constructor is executed automatically.

So constructor is only called once at the start when we are creating the object. Basically the purpose of the constructor is for to essentially initialize the object.


NOTE:

  • Constructor does not have any return type.
  • Constructor name is same as class name.
  • Constructor is called when an object of a classs is created.
  • Access modifiers applicable for constructors are public, private, protected and default.

Types of constructor

  • Default constructor
  • User defined constructor
  • Parameterised constructor

Default constructor

Default constructor is a constructor when if a class do not have constructor then compiler creates default constructor with no arguments. Let’s see an example on default constructor. Here’s how it looks in .class file,

// Employee.java
class Employee
{
   int a;
   public static void main(String args[])
   {
      // invoking default constructor
      Employee emp = new Employee();
      System.out.println(emp.a);   // Output = 0
   }
}

// Employee.class
class Employee
{
   Employee()
   {

   }
   public static void main(String args[])
   {
      Employee emp = new Employee();
   }
}

User defined constructor

User defined constructor is a constructor where user defines a constructor with no arguments. Here’s an example on user defined constructor.

class Employee
{
   Employee()
   {
      System.out.println("user-defined constructor");   
   }
   public static void main(String args[])
   {
      // invoking user defined constructor
      Employee emp = new Employee();
   }
}

Output:

user-defined constructor


Parameterized constructor

Parameterized constructor is a constructor created by user which has fixed number of parameters. Let’s see an example on parameterized constructor.

class Employee
{
   // parameterized constructor
   Employee(int ID)
   {
      System.out.println("parameterized constructor");
   }
   public static void main(String args[])
   {
      Employee obj = new Employee(1005);
   }
}

Output:

parameterized constructor


Constructor overloading in java

Constructor overloading is an approach of having more than one constructors in a class with different arguments performing different tasks.

Constructors can be overloaded like methods. Let’s see constructor overloading program in java.

class Employee
{
   int empID;  
   String empName;  
   int empAge;  
   // two argument constructor  
   public Employee(int id, String name)
   {  
      empID = id;  
      empName = name;  
   }  
   // three argument constructor  
   public Employee(int id, String name, int age)
   {  
      empID = id;  
      empName = name;  
      empAge = age;  
   }  
   void print()
   {
      System.out.println(empID + " " + empName + " " + empAge);
   }
   public static void main(String args[])
   {  
      Employee obj1 = new Employee(232, "dhoni");  
      Employee obj2 = new Employee(233, "virat", 26);  
      obj1.print();
      obj2.print();
   }
}

Output:

232 dhoni 0
233 virat 26


Constructor chaining in java

Constructor chaining is a procedure of calling one constructor from another constructor on initializing an object.

class ConstructorChainingExample 
{
   // first constructor
   ConstructorChainingExample()
   {
      this(6); // calls second constructor
      System.out.println("Default constructor");
   }
   // second constructor
   ConstructorChainingExample(int a)
   {
      this(2, 4); // calls third constructor
      System.out.println(a);
   }
   // third constructor
   ConstructorChainingExample(int a, int b)
   {
      System.out.println(a * b);
   }
   public static void main(String[] args) 
   {
      new ConstructorChainingExample();
   }
}

Output:

8
6
Default constructor

Constructor chaining rules:

  • constructor chaining can be in any order.
  • there should be atleast one constructor without this().
  • this() should be the first line in constructor.