0
0
Javaprogramming~5 mins

Constructor chaining in Java

Choose your learning style9 modes available
Introduction

Constructor chaining helps reuse code inside a class by calling one constructor from another. It makes creating objects easier and avoids repeating code.

When you have multiple constructors with different parameters but similar setup steps.
When you want to provide default values for some parameters while allowing others to be set.
When you want to simplify object creation by reusing existing constructor logic.
When you want to keep your code clean and avoid duplication in constructors.
Syntax
Java
public class ClassName {
    // Constructor with parameters
    public ClassName(Type1 param1, Type2 param2) {
        // initialization code
    }

    // Constructor chaining: calling another constructor
    public ClassName(Type1 param1) {
        this(param1, defaultValueForParam2); // calls the other constructor
    }
}

Use this(...) to call another constructor in the same class.

The call to this(...) must be the first statement in the constructor.

Examples
This example shows three constructors. The default constructor calls the single-parameter constructor, which calls the three-parameter constructor. This avoids repeating code.
Java
public class Box {
    int width, height, depth;

    // Constructor with all dimensions
    public Box(int width, int height, int depth) {
        this.width = width;
        this.height = height;
        this.depth = depth;
    }

    // Constructor chaining: cube with equal sides
    public Box(int side) {
        this(side, side, side); // calls the 3-parameter constructor
    }

    // Default constructor
    public Box() {
        this(1); // calls the single parameter constructor
    }
}
Here, constructors chain to provide default values for missing parameters.
Java
public class Person {
    String name;
    int age;

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public Person(String name) {
        this(name, 0); // default age 0
    }

    public Person() {
        this("Unknown"); // default name
    }
}
Constructor chaining is optional. If you don't need it, constructors can be independent.
Java
public class EmptyExample {
    public EmptyExample() {
        // No chaining here
    }
}
Sample Program

This program shows constructor chaining in the Car class. Different constructors call each other to set default values. The main method creates cars with different constructors and prints their details.

Java
public class Car {
    String brand;
    String model;
    int year;

    // Constructor with all parameters
    public Car(String brand, String model, int year) {
        this.brand = brand;
        this.model = model;
        this.year = year;
    }

    // Constructor chaining: default year
    public Car(String brand, String model) {
        this(brand, model, 2024); // default year
    }

    // Constructor chaining: default brand and year
    public Car(String model) {
        this("Unknown", model, 2024);
    }

    // Default constructor
    public Car() {
        this("Unknown", "Unknown", 2024);
    }

    public void printDetails() {
        System.out.println("Brand: " + brand + ", Model: " + model + ", Year: " + year);
    }

    public static void main(String[] args) {
        Car car1 = new Car("Toyota", "Corolla", 2020);
        Car car2 = new Car("Honda", "Civic");
        Car car3 = new Car("Mustang");
        Car car4 = new Car();

        System.out.println("Before constructor chaining:");
        car1.printDetails();
        car2.printDetails();
        car3.printDetails();
        car4.printDetails();
    }
}
OutputSuccess
Important Notes

Constructor chaining improves code reuse and reduces errors by centralizing initialization.

Time complexity is not affected by chaining; it is just a call to another constructor.

Common mistake: forgetting that this(...) must be the first statement in the constructor.

Use constructor chaining when you want to provide multiple ways to create an object with some default values.

Summary

Constructor chaining lets one constructor call another to reuse code.

Use this(...) as the first line in a constructor to chain.

It helps keep code clean and avoid repeating initialization steps.