• In Java another constructor of the same class can be called from a constructor via this() . Note however that this has to be on the first line. Within a constructor, we can use the this keyword to invoke another constructor in the same class. Doing so is called an explicit constructor invocation.

Constructor Chaining in Java:

  • In Java, we can call one constructor from another and it’s known as constructor chaining in Java.
  • this and super keyword is used to call one constructor from other in Java.
  • this() can be used to call another constructor of same class while super() can be used to call a constructor from super class in Java.
  • this() in reality calls no argument constructor of the same class while this(2) calls another constructor of the same class which accepts one integer parameter.
  • super() can be used to call no argument constructor of super class and super with parameter can be used to call other overloaded constructors of parent class.
  • Calling one constructor from other is called constructor chaining in Java
  • Constructor chaining is also used to implement telescoping pattern where an object can be created with combination of multiple property.

Example Program

  • In the below example the class “ChainingDemo” has 4 constructors and we are calling one constructor from another using this() statement.
  • For e.g. in order to call a constructor with single string argument we have supplied a string in this() statement like this(“hello”).

Note: this() should always be the first statement in constructor otherwise you will get the below error message:

Exception in thread “main” java.lang.Error: Unresolved compilation problem:

  • Constructor call must be the first statement in a constructor
[pastacode lang=”java” manual=”package%20kaashivwiki.com%3B%0Apublic%20class%20ChainingDemo%20%7B%0A%20%20%20%2F%2Fdefault%20constructor%20of%20the%20class%0A%20%20%20public%20ChainingDemo()%7B%0A%20%20%20%20%20%20%20%20%20System.out.println(%22Default%20constructor%22)%3B%0A%20%20%20%7D” message=”Java Code” highlight=”” provider=”manual”/] [ad type=”banner”] [pastacode lang=”java” manual=”public%20ChainingDemo(String%20str)%7B%0A%20%20%20%20%20%20%20%20%20this()%3B%0A%20%20%20%20%09%20System.out.println(%22Parametrized%20constructor%20with%20single%20param%22)%3B%0A%20%20%20%7D%0A%20%20%20public%20ChainingDemo(String%20str%2C%20int%20num)%7B%0A%20%20%20%20%20%20%20%20%20%2F%2FIt%20will%20call%20the%20constructor%20with%20String%20argument%0A%20%20%20%20%09%20this(%22Hello%22)%3B%20%0A%20%20%20%20%09%20System.out.println(%22Parametrized%20constructor%20with%20double%20args%22)%3B%0A%20%20%20%7D%0A%20%20%20public%20ChainingDemo(int%20num1%2C%20int%20num2%2C%20int%20num3)%7B%0A%20%20%20%09%2F%2F%20It%20will%20call%20the%20constructor%20with%20(String%2C%20integer)%20arguments%0A%20%20%20%20%20%20%20%20this(%22Hello%22%2C%202)%3B%0A%20%20%20%20%20%20%20%20System.out.println(%22Parametrized%20constructor%20with%20three%20args%22)%3B%0A%20%20%20%7D%0A%20%20%20public%20static%20void%20main(String%20args%5B%5D)%7B%0A%20%20%20%20%20%20%20%20%2F%2FCreating%20an%20object%20using%20Constructor%20with%203%20int%20arguments%0A%20%20%20%20%20%20%20%20ChainingDemo%20obj%20%3D%20new%20ChainingDemo(5%2C5%2C15)%3B%0A%20%20%20%7D%0A%7D” message=”Java Code” highlight=”” provider=”manual”/]

Output:

Default constructor
Parametrized constructor with single param
Parametrized constructor with double args
Parametrized constructor with three args

Another example of constructor chaining which shows How to call overloaded constructor of same class and parent class in Java.

[pastacode lang=”java” manual=”public%20class%20ConstructorChainingExample%20%7B%0A%20%20%0A%20%20%0A%20%20%20%20public%20static%20void%20main(String%20args%5B%5D)%20%7B%0A%20%20%20%20%0A%20%20%20%20%20%20%20%20%2F%2Fthis%20will%20first%20call%20one%20argument%20constructor%20of%20Child%20Class%20which%0A%20%20%20%20%20%20%20%20%2F%2Fin%20turn%20call%20corresponding%20constructor%20of%20super%20class%20using%20super(String)%0A%20%20%20%20%20%20%20%20System.out.println(%22Constructor%20chaining%20Example%20in%20Java%22)%3B%0A%20%20%20%20%20%20%20%20Child%20child%20%3D%20new%20Child(%22Jeremy%22)%3B%0A%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20%2F%2Fthis%20constructor%20will%20call%20no%20argument%20constructor%20of%20Child%2C%0A%20%20%20%20%20%20%20%20%2F%2Fwhich%20then%20call%20one%20argument%20constructor%20of%0A%20%20%20%20%20%20%20%20%2F%2Fsame%20class%2C%20which%20finally%20call%20corresponding%20one%20argument%20constructor%0A%20%20%20%20%20%20%20%20%2F%2F%20of%20super%20class%20Parent.%0A%20%20%20%20%20%20%20%20System.out.println(%22———————————%22)%3B%0A%20%20%20%20%20%20%20%20Child%20emptyChild%20%3D%20new%20Child()%3B%0A%20%20%20%20%7D%20%0A%20%20%0A%7D” message=”Java Code” highlight=”” provider=”manual”/] [pastacode lang=”java” manual=”class%20Parent%7B%0A%20%20%20%20private%20String%20name%3B%0A%20%20%20%20%2F*%20*%20Calling%20constructor%20of%20same%20class%20with%20one%20String%20argument*%2F%0A%20%20%20%20protected%20Parent()%7B%0A%20%20%20%20%20%20%20%20this(%22%22)%3B%0A%20%20%20%20%20%20%20%20System.out.println(%22No%20argument%20constructor%20of%20Parent%20called%20%22)%3B%0A%20%20%20%20%7D%0A%20%20%0A%20%20%20%20protected%20Parent(String%20name)%7B%0A%20%20%20%20%20%20%20%20this.name%20%3D%20name%3B%0A%20%20%20%20%20%20%20%20System.out.println(%22One%20String%20argument%20constructor%20of%20Parent%20called%20%22)%3B%0A%20%20%20%20%7D%0A%7D” message=”Java Code” highlight=”” provider=”manual”/] [ad type=”banner”] [pastacode lang=”java” manual=”class%20Child%20extends%20Parent%7B%0A%20%20%20%20private%20String%20name%3B%0A%20%20%0A%20%20%20%20%2F**%20Calling%20constructor%20same%20class%20with%20one%20argument%20*%2F%0A%20%20%20%20protected%20Child()%7B%0A%20%20%20%20%20%20%20%20this(%22%22)%3B%0A%20%20%20%20%20%20%20%20System.out.println(%22No%20argument%20constructor%20of%20Child%20called%20%22)%3B%0A%20%20%20%20%7D” message=”Java Code” highlight=”” provider=”manual”/] [pastacode lang=”java” manual=”%2F*%09*%20Calling%20constructor%20of%20super%20class%20with%20one%20argument%0A%20%20%20%20%20%09*%20call%20to%20super()%20must%20be%20first%20line%20in%20constructor%0A%20%20%20%20%20%09*%2F%0A%20%20%20%20protected%20Child(String%20name)%7B%0A%20%20%20%20%20%20%20%20super(name)%3B%0A%20%20%20%20%20%20%20%20System.out.println(%22One%20argument%20constructor%20of%20Super%20class%20called%20from%20sub%20class%20%22)%3B%0A%20%20%20%20%7D%0A%7D” message=”Java Code” highlight=”” provider=”manual”/]

OUTPUT:

Constructor chaining Example in Java
One String argument constructor of Parent called
One argument constructor of Super class called from sub class
———————————
One String argument constructor of Parent called
One argument constructor of Super class called from sub class
No argument constructor of Child called

Categorized in: