Constructor Chaining In Java with Examples

Last Updated : 17 Jun, 2026

Constructor chaining is the process of calling one constructor from another constructor within the same class or from a parent class. It helps reduce code duplication and improves code readability by reusing existing constructor logic. Constructor chaining is achieved using the this() and super() keywords.

  • Uses this() for constructors within the same class.
  • Uses super() to call a parent class constructor.

Types of Constructor Chaining in Java

1. Constructor Chaining Within the Same Class Using this()

Constructor chaining within the same class occurs when one constructor calls another constructor of the same class using the this() keyword. It is mainly used to reuse initialization code between multiple constructors.

  • this() calls another constructor of the same class.
  • It must be written as the first statement in a constructor.
Constructor Chaining In Java Java
class Temp
{
    // default constructor 1
    // default constructor will call another constructor
    // using this keyword from same class
    Temp()
    {
        // calls constructor 2
        this(5);
        System.out.println("The Default constructor");
    }

    // parameterized constructor 2
    Temp(int x)
    {
        // calls constructor 3
        this(5, 15);
        System.out.println(x);
    }

    // parameterized constructor 3
    Temp(int x, int y)
    {
        System.out.println(x * y);
    }

    public static void main(String args[])
    {
        // invokes default constructor first
        new Temp();
    }
}

Output
75
5
The Default constructor

Rules of constructor chaining : 

  • The this() expression should always be the first line of the constructor.
  • There should be at-least be one constructor without the this() keyword (constructor 3 in above example).
  • Constructor chaining can be achieved in any order.

What happens if we change the order of constructors?

Nothing, Constructor chaining can be achieved in any order.

Example: Java program to illustrate Constructor Chaining within same class Using this() keyword and changing order of constructors

Java
class Temp
{
    // default constructor 1
    Temp()
    {
        System.out.println("default");
    }

    // parameterized constructor 2
    Temp(int x)
    {
        // invokes default constructor
        this();
        System.out.println(x);
    }

    // parameterized constructor 3
    Temp(int x, int y)
    {
        // invokes parameterized constructor 2
        this(5);
        System.out.println(x * y);
    }

    public static void main(String args[])
    {
        // invokes parameterized constructor 3
        new Temp(8, 10);
    }
}

Output
default
5
80

NOTE: In example 1, default constructor is invoked at the end, but in example 2 default constructor is invoked at first. Hence, order in constructor chaining is not important. 

2. Constructor Chaining Using super() (Parent Class) 

Constructor chaining between classes occurs when a child class constructor calls the parent class constructor using the super() keyword. It ensures that the parent class object is initialized before the child class object.

  • It must be the first statement in the child class constructor.
  • Used in inheritance to initialize superclass members.

Syntax

class Parent {
Parent() {
// parent constructor
}
}

class Child extends Parent {
Child() {
super();
// child constructor
}
}

Java
class Base
{
    String name;

    // constructor 1
    Base()
    {
        this("");
        System.out.println("No-argument constructor of" + 
                                           " base class");
    }

    // constructor 2
    Base(String name)
    {
        this.name = name;
        System.out.println("Calling parameterized constructor" 
                                              + " of base");
    }
}

class Derived extends Base
{
    // constructor 3
    Derived()
    {
        System.out.println("No-argument constructor " + 
                           "of derived");
    }

    // parameterized constructor 4
    Derived(String name)
    {
        // invokes base class constructor 2
        super(name);
        System.out.println("Calling parameterized " + 
                           "constructor of derived");
    }

    public static void main(String args[])
    {
        // calls parameterized constructor 4
        Derived obj = new Derived("test");

        // Calls No-argument constructor
        // Derived obj = new Derived();
    }
}

Output
Calling parameterized constructor of base
Calling parameterized constructor of derived

Note : Similar to constructor chaining in same class, super() should be the first line of the constructor as super class's constructor are invoked before the sub class's constructor.

Constructor Chaining Using Initialization Block

An initialization block is a block of code that executes before constructors whenever an object is created. It is useful when the same code needs to run for multiple constructors.

  • Used to initialize common resources.
  • Multiple initialization blocks execute in the order they are defined.
Java
class Temp
{
    // block to be executed before any constructor.
    {
        System.out.println("init block");
    }

    // no-arg constructor
    Temp()
    {
        System.out.println("default");
    }

    // constructor with one argument.
    Temp(int x)
    {
        System.out.println(x);
    }

    public static void main(String[] args)
    {
        // Object creation by calling no-argument 
        // constructor.
        new Temp();

        // Object creation by calling parameterized 
        // constructor with one parameter.
        new Temp(10);
    }
}

Output
init block
default
init block
10

NOTE: If there are more than one blocks, they are executed in the order in which they are defined within the same class

Java
class Temp
{
    // block to be executed first
    {
        System.out.println("init");
    }
    Temp()
    {
        System.out.println("default");
    }
    Temp(int x)
    {
        System.out.println(x);
    }

    // block to be executed after the first block
    // which has been defined above.
    {
        System.out.println("second");
    }
    public static void main(String args[])
    {
        new Temp();
        new Temp(10);
    }
}

Output
init
second
default
init
second
10

Advantages of Constructor Chaining

  • Avoids code duplication
  • Improves code readability
  • Makes constructor management easier
  • Provides better object initialization
  • Supports inheritance-based initialization
Comment