Constructor chaining helps reuse code inside a class by calling one constructor from another. It makes creating objects easier and avoids repeating code.
Constructor chaining in 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.
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 } }
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 } }
public class EmptyExample { public EmptyExample() { // No chaining here } }
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.
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(); } }
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.
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.