Constructor Chaining in Java

Constructor chaining is the process of calling one constructor from another constructor within the same class, or from a constructor in the superclass. It's done using the this() and super() keywords.

Chaining Within the Same Class — this()

class Student {
    String name;
    int age;

    Student() {
        this("Unknown", 0);
        System.out.println("Default constructor called");
    }

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

Chaining to the Superclass — super()

class Person {
    Person(String name) {
        System.out.println("Person constructor: " + name);
    }
}

class Employee extends Person {
    Employee(String name) {
        super(name);
        System.out.println("Employee constructor");
    }
}

Rules of Constructor Chaining

  • this() or super() must be the first statement in a constructor
  • A constructor cannot call both this() and super() at the same time
  • If neither is written explicitly, Java inserts an implicit call to super()

Why Use Constructor Chaining?

  • Avoids duplicating initialization code across multiple constructors
  • Ensures a common initialization path is always followed
  • Makes it easier to maintain default values in one place
Constructor chaining within the same class must use this(), while chaining to a parent class constructor must use super() — they can never be mixed in the same call.

Master Java with Uncodemy

Hands-on training, live projects, and placement support in our Java Programming Course.

Explore the Course

Tracing the Order of Execution

When a chain spans several classes, Java always builds an object from the top of the hierarchy down. Running the example below makes the order visible.

class Vehicle {
    Vehicle() {
        System.out.println("1. Vehicle ready");
    }
}

class Car extends Vehicle {
    String model;

    Car() {
        this("Basic");                       // jump to Car(String)
        System.out.println("4. Car() body done");
    }

    Car(String model) {
        super();                             // jump to Vehicle()
        System.out.println("2. Car(String) running");
        this.model = model;
        System.out.println("3. Model set to " + model);
    }
}

public class Demo {
    public static void main(String[] args) {
        new Car();
    }
}

Output order: Vehicle ready, Car(String) running, Model set to Basic, Car() body done. The call goes down the chain first, then each constructor body finishes on the way back. Field initialisers in each class run right after its superclass constructor returns, before the rest of that class's constructor body.

A Practical Use: Sensible Defaults

Constructor chaining shines when a class offers several ways to be created. Keep all real work in the most complete constructor and let the others fill in defaults.

public class HttpRequest {
    private final String url;
    private final String method;
    private final int timeoutSeconds;

    public HttpRequest(String url) {
        this(url, "GET");
    }

    public HttpRequest(String url, String method) {
        this(url, method, 30);
    }

    public HttpRequest(String url, String method, int timeoutSeconds) {
        if (url == null || url.isBlank()) {
            throw new IllegalArgumentException("url is required");
        }
        this.url = url;
        this.method = method;
        this.timeoutSeconds = timeoutSeconds;
    }
}

Validation lives in one place, so every creation path is checked the same way.

Alternatives When Chains Get Long

If a class needs many optional values, a long chain of constructors becomes hard to read. Two common alternatives are:

  • Static factory methods with descriptive names, such as HttpRequest.get(url).
  • The Builder pattern, where you set only the values you need and call build().

Errors Beginners Hit

  • Recursive constructor invocation: two constructors calling each other with this(); the compiler rejects it.
  • Missing no-argument parent constructor: if the parent defines only Person(String name), a child constructor without an explicit super(name) fails to compile.
  • Calling an overridable method from a constructor: the child's override may run before the child's fields are set.
  • Using instance fields as arguments to this() or super(), which is not allowed because the object is not ready yet.

Chaining and Records

Java records, which are compact classes for holding data, follow the same rule. A record has one canonical constructor generated from its components, and any extra constructor you add must call another constructor of the record with this(...) as its first statement.

record Point(int x, int y) {
    Point() {
        this(0, 0);   // must delegate to the canonical constructor
    }
}

This is constructor chaining enforced by the language, which guarantees that every record is created through one validated path. It is a good example of why the pattern matters: when all roads lead to one constructor, there is exactly one place to check rules and set fields. Keep that idea in mind when designing your own classes, even ordinary ones.