As you know we can overload multiple constructors in a class but at the time of an object creation only one constructor gets called. There are some situations in programming where we need to call multiple constructors from one another without creating multiple objects. The mechanism of calling a constructor from another constructor and that called constructor calls another constructor is called constructor chaining.
Constructor chaining is for code maintenance, basically the idea is that you only write one piece of code once, and there is no risk of overlooking something when editing your methods and the two accidentally becoming different.
The constructor being called doesnt't create a different object at all, it uses current running object to call another constructor. There is no need of new keyword so new object does not get created. Basically it is like you're just calling a different method inside your constructor, except that method happens to also be a constructor.
For constructor chaining we will require this() and super().
this() – The method representation of this keyword will call a current class constructor.
super() – The method representation of super keyword will call a immediate parent/super class constructor.
Constructor Invocation: If you call a constructor and that constructor got called then it is known as constructor invocation.
Constructor Execution: If after invoking a constructor control starts executing the statements in constructor body is called constructor execution.
this() can call same class constructor only.
super() can call immediate super class constructor only.
this() and super() must be the first statement in the constructor.
We can not use this() and super() in a same constructor, because both must be a first statement in the constructor and that is not possible.
We cannot add this() in all the constructors of a same class, there should be atleast one constructor without this() statement.
If you did not add this() or super() in a constructor then Java compiler implicitly add super() statement in the constructor that will call immediate super class default or no- argument constructor.
The constructor invocation and execution happens exactly opposite, means if we called the constructors in 1, 2, 3 order then the execution will be in 3, 2, 1 order.
Example:
In the following constructor chaining example, we have taken two classes which are overloading multiple constructors.
We want to execute the constructors in 2, 1, 4, 5, 3 order.
To execute constructors in the order, we need to call the constructors exactly in opposite order like 3, 5, 4, 1, 2, see in the below example.
class ParentClass
{
ParentClass()// 1
{
this("hi");
System.out.println("in ParentClass 0 arg constructor 1");
}
ParentClass(String msg)// 2
{
System.out.println("in ParentClass(String) constructor 2");
}
}
class ConstructorChain extends ParentClass
{
ConstructorChain()// 3
{
this(10);// calling 5 number constructor
System.out.println("in ConstructorChain 0 arg constructor 3");
}
ConstructorChain(String msg)// 4
{
super();//calling 1 number parent class constructor
System.out.println("in ConstructorChain(String msg) arg constructor 4");
}
ConstructorChain(int i)// 5
{
this("hi");//calling 4 number constructor
System.out.println("in ConstructorChain(int i) constructor 5");
}
// main() method
public static void main(String[] args)
{
ConstructorChain cobj = new ConstructorChain(); // calling 3 number constructor
}
}
Output:
in ParentClass(String) constructor 2
in ParentClass 0 arg constructor 1
in ConstructorChain(String msg) arg constructor 4
in ConstructorChain(int i) constructor 5
in ConstructorChain 0 arg constructor 3