Constructor Chaining in Java

Constructor Chaining in Java

15 Jul 2025
Beginner
3.41K Views
21 min read
Learn with an interactive course and practical hands-on labs

Free Java Course With Certificate

Constructor chaining in Java is a process where one constructor calls another constructor, either in the same class using this() or in the parent class using super(). This technique ensures efficient code reuse and consistent object initialization by eliminating repetitive constructor logic. It plays a crucial role in simplifying complex object creation and maintaining cleaner, more maintainable Java code.
In this Java Tutorial, we'll explore What is Constructor Chaining in Java?, What is the use of Constructor Chaining?, Constructor Chaining in Java with Examples.

What is constructor chaining in Java?

We assume you already know what are Constructors in Java. They are the special methods which help in initializing newly created objects. One of the concepts related to constructors is Constructor Chaining.

  • Constructor Chaining is a mechanism in Java where one constructor calls another constructor within the same class or in its superclass.
  • It works like a chain, where one constructor leads to another and the execution of multiple constructors occurs in a sequence.
  • To chain constructors within the same class, we use this() keyword and super() to call constructor from the parent class.

Why Do We Need Constructor Chaining in Java?

Constructor Chaining in Java is needed to streamline the process of object creation when a class has multiple constructors with different parameters. Instead of writing the same initialization logic in every constructor, Java allows one constructor to call another using this() for the current class or super() for the parent class. This reduces code duplication and makes your code cleaner and easier to maintain.

Using constructor chaining improves code reusability, enhances readability, and ensures that initialization happens in a consistent and logical order. It also helps in avoiding common programming errors by centralizing shared setup logic in a single place. This is especially useful in real-world applications where classes often need to be initialized in different ways based on various conditions.

Read More - Advanced Java Interview Questions And Answers

Read More - Java Multithreading Interview Questions

Types of Constructor Chaining

  1. Chaining within the same class this type of constructor chaining in Java, chaining occurs within the same class, that is, a constructor calls another constructor of the same class to which it belongs.
  2. Syntax

        public class MyClass {
        // Constructor with parameters
        public MyClass(int param1, String param2) {
            // Some initialization or processing
            
            // Call another constructor within the same class
            this(param1);
            
            // Additional initialization or processing
        }
        
        // Constructor with a different set of parameters
        public MyClass(int param1) {
            // Some initialization or processing
            
            // Additional initialization or processing
        }
    }    
  3. Chaining with Superclass Constructors- With the help of Inheritance, a subclass can inherit from a superclass. Constructor Chaining with superclass constructors is when a constructor in a subclass calls a constructor from its superclass using the ‘super()’ keyword. This ensures proper execution of initialization tasks in both superclass and subclass when an object is created in the subclass.
  4. Syntax

        public class Subclass extends Superclass {
        // Constructor with parameters
        public Subclass(int param1, String param2) {
            // Call superclass constructor with parameters
            super(param1, param2);
            
            // Additional initialization or processing
        }
    }    

How Constructor Chaining Works in Java?

Constructor Chaining in Java can be seen as a teamwork approach where each constructor helps set up the object, and together they ensure everything is ready for action. For instance, if you have multiple constructors in a class, one constructor can pass the job of setting up an object to another constructor within the same class or in its parent class.
Constructor Chaining in Java

Example on our Java Compiler

    public class MyClass {
    private int value1;
    private String value2;

    // Constructor with parameters
    public MyClass(int value1, String value2) {
        // Call another constructor within the same class
        this(value1);

        // Some initialization or processing
        this.value2 = value2;

        // Additional initialization or processing
        System.out.println("Constructor with int and String parameters called.");
    }

    // Constructor with a different set of parameters
    public MyClass(int value1) {
        // Some initialization or processing
        this.value1 = value1;

        // Additional initialization or processing
        System.out.println("Constructor with int parameter called.");
    }

    public void displayValues() {
        System.out.println("Value1: " + value1);
        System.out.println("Value2: " + value2);
    }

    public static void main(String[] args) {
        // Creating an instance of MyClass
        MyClass obj = new MyClass(10, "Hello");

        // Displaying values
        obj.displayValues();
    }
}    

In the above example,

  • There are two constructors in the class MyClass, one has two parameters and another only one.
  • The constructor that has two parameters, int and string, calls the other constructor that has only one parameter, int using the this() keyword.

Output

Constructor with int parameter called.
Constructor with int and String parameters called.
Value1: 10
Value2: Hello

Chaining Constructors in Superclasses

Constructor chaining is not limited to classes with a single level of inheritance. It can also be used in subclasses to chain constructors from their superclass.
Consider the following example in the Java Online Compiler:
    class Vehicle {
    private String brand;
    private String model;

    // Constructor with parameters
    public Vehicle(String brand, String model) {
        this.brand = brand;
        this.model = model;
        System.out.println("Vehicle constructor called with brand: " + brand + ", model: " + model);
    }
}

class Car extends Vehicle {
    private int year;

    // Constructor with parameters
    public Car(String brand, String model, int year) {
        super(brand, model); // Call superclass constructor with parameters
        this.year = year;
        System.out.println("Car constructor called with brand: " + brand + ", model: " + model + ", year: " + year);
    }
}

public class Main {
    public static void main(String[] args) {
        // Creating an instance of Car
        Car car = new Car("Toyota", "Corolla", 2020);
    }
}    

In the above example,

  • There is a superclass Vehicle, which has attributes as brandand model.
  • It has a subclass Car, with another attribute year.
  • Using the super() keyword, the constructor of the Car class calls the constructor of Vehicle class.
  • In the main()method, when an instance of Caris created, both the constructors of Car and Vehicle are called.

Output

Vehicle constructor called with brand: Toyota, model: Corolla
Car constructor called with brand: Toyota, model: Corolla, year: 2020

Implicit Super Constructor Call

If there is a case where the superclass constructor is not directly called by the subclass constructor, the Java compiler will automatically do that and call the superclass’s no-argument constructor.

Example:

    class Superclass {
    int num;

    // Superclass constructor
    public Superclass() {
        System.out.println("Superclass constructor called.");
        num = 10;
    }
}

class Subclass extends Superclass {
    // Subclass constructor
    public Subclass() {
        // Call to superclass constructor
        super();
        System.out.println("Subclass constructor called.");
    }

    // Method to display the value of num
    public void display() {
        System.out.println("Value of num in superclass: " + num);
    }
}

public class Main {
    public static void main(String[] args) {
        // Creating an object of Subclass
        Subclass obj = new Subclass();

        // Calling display method
        obj.display();
    }
}    

Explanation:

  • We have two classes here, ‘Superclass’ and ‘Subclass’ in our Java Online Editor.
  • Subclass inherits from Superclass.
  • Superclass has a default constructor that initializes the variable ‘num’ to 10.
  • Subclass does not define any constructors explicitly.
  • When an object of Subclass is created (‘Subclass obj = new Subclass();’), the constructor of Superclass is implicitly called before the constructor of Subclass.
  • The Java compiler calls the superclass constructor implicitly by itself and ‘Superclass’ execution occurs first, before the execution of the ‘Subclass’.

Output

Superclass constructor called.
Subclass constructor called.
Value of num in superclass: 10

Advantages of Constructor Chaining

Advantages of constructor chaining

  • Code Reusability – Constructor chaining enhances code reusability by eliminating duplicate initialization logic. Instead of rewriting similar code in multiple constructors, one constructor can call another, streamlining the object creation process.
  • Improved Code Organization – By chaining constructors, your Java code becomes cleaner and better organized. It enhances readability and helps developers quickly understand how different constructors relate to one another.
  • Enhanced Flexibility – Constructor chaining supports multiple constructors with different parameter sets, offering flexibility in how objects are initialized. This allows developers to provide varied ways of creating objects based on specific use cases.
  • Proper Initialization Order – When constructors are chained, it ensures that superclass constructors or common initialization logic execute before subclass-specific or specialized logic. This guarantees that objects are always initialized in the correct and consistent order.

Rules of Constructor Chaining
When using constructor chaining in Java, it’s important to follow specific rules to ensure proper implementation and avoid compilation errors. Below are the key rules you should always keep in mind:
  • Constructor Call Must Be the First Statement-  The call to another constructor—either using this() (same class) or super() (parent class)—must be the very first statement inside a constructor. Placing it elsewhere will result in a compilation error.
  • Only One Constructor Call Allowed-  A constructor can only invoke one other constructor. This means you can’t use both this() and super() in the same constructor. Only a single constructor call is permitted.
  • No Circular Constructor Calls-  Constructor chaining must not result in a circular chain, where a constructor ends up calling itself either directly or indirectly. This will cause an infinite loop and result in a compile-time error or runtime exception.

Using the init() method for Constructor Chaining

There is an alternate method used for Constructor Chaining in Java which is the init() method. Using the init block too, you can use Constructor Chaining in Java. Just see the working in our Java Playground.

Example

    class MyClass {
    private int value1;
    private String value2;

    // Constructor with parameters
    public MyClass(int value1, String value2) {
        // Perform some initialization or processing
        init(value1, value2);
        System.out.println("Constructor with int and String parameters called.");
    }

    // Constructor with a different set of parameters
    public MyClass(int value1) {
        // Perform some initialization or processing
        init(value1, "Default");
        System.out.println("Constructor with int parameter called.");
    }

    // Private initialization method
    private void init(int value1, String value2) {
        this.value1 = value1;
        this.value2 = value2;
        // Additional initialization or processing can be done here
    }

    public void displayValues() {
        System.out.println("Value1: " + value1);
        System.out.println("Value2: " + value2);
    }

    public static void main(String[] args) {
        // Creating an instance of MyClass
        MyClass obj1 = new MyClass(10, "Hello");
        MyClass obj2 = new MyClass(20);

        // Displaying values
        obj1.displayValues();
        obj2.displayValues();
    }
}    

In the above example,

  • We have used the private init() method for initializing the instance variables.
  • There are two constructors in MyClass and both of them call the init method with parameters needed.

Output

Constructor with int and String parameters called.
Constructor with int parameter called.
Value1: 10
Value2: Hello
Value1: 20
Value2: Default 

Common errors in Constructor chaining

Constructor chaining is helpful, but beginners often make some common mistakes. Here's a simple list of what to avoid:

  •   Constructor Calling Itself Again and Again- This happens when two or more constructors keep calling each other in a loop. It causes the program to crash with an error.
  •  Wrong Placement of this() or super()- In Java, the this() or super() call must always be the first line inside a constructor. Placing it after any other line causes an error.
  •   Using Both this() and super() Together- You can only use one constructor call at the start. If you try to use both this() and super() in the same constructor, Java will show an error.
  •   Calling a Constructor That Doesn’t Exist- If you try to call a constructor with parameters that you haven’t defined, Java won’t understand it and will give a compile-time error.
Summary
Through this Java Tutorial, we hope you got a knack of what Constructor Chaining is, how it works and what are its benefits. Understanding how Constructor chaining works in Java, will surely help you get better with your Java programs. To have more expertise in Java, do consider enrolling in our Java Certification Course Free.

FAQs

Constructor Chaining is used to avoid code duplication and ensure proper initialization by allowing one constructor to call another within the same class or its superclass improving code creation and organization.

To call one constructor from another in Java:
  1. Use ‘this()’ keyword with appropriate arguments to call another constructor in the same class.
  2. Ensure that the call to another constructor is the first statement in the constructor where it is called.

Constructor chaining in Java allows one constructor to call another constructor within the same class or its superclass, enabling code reuse and ensuring proper initialization of objects.

Method chaining involves calling multiple methods on the same object in a single line to perform a series of operations, whereas constructor chaining involves calling one constructor from another within the same class or its superclass during object creation to initialize the object.

Take our Java skill challenge to evaluate yourself!

In less than 5 minutes, with our skill challenge, you can identify your knowledge gaps and strengths in a given skill.

GET FREE CHALLENGE

Share Article
About Author
Shailendra Chauhan (Microsoft MVP, Founder & CEO at ScholarHat)

Shailendra Chauhan, Founder and CEO of ScholarHat by DotNetTricks, is a renowned expert in System Design, Software Architecture, Azure Cloud, .NET, Angular, React, Node.js, Microservices, DevOps, and Cross-Platform Mobile App Development. His skill set extends into emerging fields like Data Science, Python, Azure AI/ML, and Generative AI, making him a well-rounded expert who bridges traditional development frameworks with cutting-edge advancements. Recognized as a Microsoft Most Valuable Professional (MVP) for an impressive 9 consecutive years (2016–2024), he has consistently demonstrated excellence in delivering impactful solutions and inspiring learners.

Shailendra’s unique, hands-on training programs and bestselling books have empowered thousands of professionals to excel in their careers and crack tough interviews. A visionary leader, he continues to revolutionize technology education with his innovative approach.
Live Training - Book Free Demo
Azure AI Engineer Certification Training
28 Aug
08:30PM - 10:30PM IST
Checkmark Icon
Get Job-Ready
Certification
Azure AI & Gen AI Engineer Certification Training Program
28 Aug
08:30PM - 10:30PM IST
Checkmark Icon
Get Job-Ready
Certification
.Net Software Architecture and Design Training
30 Aug
10:00AM - 12:00PM IST
Checkmark Icon
Get Job-Ready
Certification
.NET Solution Architect Certification Training
30 Aug
10:00AM - 12:00PM IST
Checkmark Icon
Get Job-Ready
Certification
ASP.NET Core Certification Training
31 Aug
08:30PM - 10:30PM IST
Checkmark Icon
Get Job-Ready
Certification
Accept cookies & close this