In Java, there are instances where the developer needs to integrate the code functionalities from time to time. For instance, associating the parent class with the child class to split the code functionalities, thereby improving the code readability.
This blog guides you to invoke one constructor from another in Java.
What is a Constructor?
Constructor in Java is utilized to create the class’s instance along with the usage of the “new” keyword. For instance, the constructor of the class “Sample” will be specified as “Sample()” which creates an object of this class.
How to Call/Invoke One Constructor From Another in Java?
The calling of one constructor from another can be done in the following different ways:
- Directly via the “this” Keyword.
- Invoking the Parent Class’s Constructor from the Child Class’s Constructor Using the “super” Keyword.
Example 1: Calling One Constructor From Another
The following code example includes a constructor that invokes a parameterized constructor:
class Const {
int x;
Const () {
this(3, 5);
}
Const (int val1, int val2) {
this.x = val1 * val2;
}
void out() {
System.out.println("Multiplication -> " + x);
}}
public class Constructorcalling {
public static void main(String[] args) {
Const obj = new Const();
obj.out();
}}
According to these code lines:
- First of all, define the class “Const” comprising a specified variable.
- After that, create a class constructor invoking the parameterized constructor defined later by passing the stated integer values to it via “this”.
- Now, create a parameterized class constructor that returns the multiplication of the provided integers.
- Also, declare the function “out()” for displaying the corresponding multiplication of values.
- Lastly, in “main”, create a class object and invoke the “out()” function to return the resultant multiplication of values via invoking one constructor from another one.
Output

Example 2: Calling Constructor of the Parent Class From the Child Class’ Constructor
In this particular example, the constructor of the parent class will be invoked via the child class’ constructor:
class Const {
Const (int val1, int val2) {
System.out.println("Multiplication -> " +val1 * val2);
}}
class childConst extends Const{
childConst(){
super(2,3);
}}
public class Constructorcalling {
public static void main(String[] args) {
childConst obj = new childConst();
}}
In this block of code:
- Define the superclass “Const” comprising the parameterized constructor that returns the multiplication of the passed integers.
- Create a child class “childConst” that inherits from its parent class utilizing the “extends” keyword.
- In the class definition, create a constructor that invokes the parent class’s constructor by passing the values into it using the “super” keyword.
- In “main”, create an object of the child class to retrieve the multiplication of the passed integers via invoking the parent class’s constructor.
Output

Conclusion
A constructor can be called from another constructor directly via the “this” keyword or invoked from the parent class’s constructor via the child class’s constructor using the “super” keyword. This article elaborated on the approaches to invoking a constructor from another one in Java.