Constructor Chaining In Java With Example

Constructor Chaining is a process of calling the one constructor from another constructor with respect to current object. Whenever we want constructor to duplicate some of the behavior of an existing constructor we use constructor chaining. In the same class referring to another constructor we use this as the method name, followed by appropriate arguments enclosed in pair of parenthesis, if necessary. Java requires that the call to this() can be used in a constructor and must be the first statement in the constructor followed by any other relevant code.


Table of Contents

Program Example of Constructor Chaining:

Let us consider a example to show use of constructor chaining.

Step 1: First create a class Temp in which we take three constructors:

class Temp

{

  Temp()  //default constructor

  {

    this(10);
    System.out.println("default constructor");

  }

  Temp(int x)  //one parameter constructor

  {

    this(10,20);
    System.out.println("value of x is" +x);

  }

  Temp(int x,int y)  //two parameter constructor

  {

    System.out.println("Add result is" +(x+y));

  }

  public static void main(String[] args)

  {

    new Temp();

  }

}

Output:

Add result is 30
Value of x is 10
Default constructor

Explanation of Program:

  • The class Temp contains three constructors: default constructor, one parameter constructor and two parameter constructor.
  • The statement : new Temp(); calls a default constructor. Here it calls the one parameter constructor with the help of this(10);
  • Then control goes to two parameter constructor with the help of first statement in constructor this(10,20),
  • In two parameter constructor the first statement will be executed which prints the “add result is 30”
  • Then control goes to one parameter constructor and it executes the second statement which prints “value of x is 10”
  • Then control goes to default constructor and it also execute the second statement that prints “default constructor”

Construtcor Chaining Example in JAVA


Difference Between Constructor Overloading and Constructor Chaining:

Constructor Overloading
Constructor Chaining
1. Constructor overloading allows a class to have more than one constructor that have the same name as that of the class but differ only in terms of number or type of parameters. 1. Constructor chaining is a process of calling the one constructor from another constructor with respect to current object.
2. Constructor overloading is done to construct object in different ways. 2. Constructor chaining is done to call one constructor from another constructor.
3. Constructor overloading is achieved by declaring more than one constructor with same name but different parameters in a same class. 3. Constructor chaining is achieved by this() method.
4. Constructor overloading is flexible which allows us to create object in different way. 4. Constructor chaining is useful when we have many constructors in the class and want to reuse that constructor.

 Important Points to Remember About Constructor Chaining:

  • Whenever you are achieving a constructor chaining using a this() method then it must be the first line in any constructor.
  • Whenever we are achieving a constructor chaining using a this() method then there must be atleast one constructor which is not having a this() method as a first line.
  • Constructor chaining can be achieved in any order.
  • Whenever you want to provide the certain common resources to each object of a class rather than putting the code of each resource in a single constructor, make a separate constructor for each resource and then create their chain.

Also Read: Constructor In Java With Example