Constructor Chaining in Java – this() and super() Explained
Constructor chaining means one constructor calling another. Java supports two kinds: calling another constructor of the same class with this(...), and calling a constructor of the parent class with super(...). Chaining removes duplicated initialisation code and guarantees that every part of an object, including the parts it inherits, is set up correctly.
This tutorial explains both forms with runnable examples, shows the order in which chained constructors run, and lists the rules the compiler enforces.
1. Why Chain Constructors?
- No duplication: validation and assignment logic lives in one constructor; the others delegate to it.
- Sensible defaults: short constructors supply default values and pass them on.
- Correct inheritance: each class initialises its own fields, after its parent has initialised the inherited ones.
2. Chaining Within a Class: this(...)
Overloaded constructors can call each other with this(...). A common pattern is to let the constructor with the most parameters do all the work, and have the others call it with default values:
public class ThisChaining {
static class Order {
final String item;
final int quantity;
final String delivery;
Order(String item) {
this(item, 1); // calls the 2-argument constructor
System.out.println(" Order(item) finished");
}
Order(String item, int quantity) {
this(item, quantity, "standard"); // calls the 3-argument constructor
System.out.println(" Order(item, quantity) finished");
}
Order(String item, int quantity, String delivery) { // all the real work happens here
if (quantity <= 0) throw new IllegalArgumentException("quantity must be positive");
this.item = item;
this.quantity = quantity;
this.delivery = delivery;
System.out.println(" Order(item, quantity, delivery) finished");
}
@Override public String toString() { return quantity + " x " + item + " (" + delivery + ")"; }
}
public static void main(String[] args) {
System.out.println("new Order(\"book\"):");
System.out.println(new Order("book"));
System.out.println("new Order(\"pen\", 5, \"express\"):");
System.out.println(new Order("pen", 5, "express"));
}
}
new Order("book"):
Order(item, quantity, delivery) finished
Order(item, quantity) finished
Order(item) finished
1 x book (standard)
new Order("pen", 5, "express"):
Order(item, quantity, delivery) finished
5 x pen (express)
Notice the output order: the most complete constructor finishes first, then control returns to each caller in turn, just like nested method calls.
3. Chaining to the Parent Class: super(...)
Every constructor of a subclass must start by running a constructor of its parent class. Use super(...) to choose which one and pass arguments. In a three-level hierarchy, the chain runs from the top of the hierarchy down:
public class SuperChaining {
static class Person {
final String name;
Person(String name) {
this.name = name;
System.out.println("1. Person constructor: name set");
}
}
static class Employee extends Person {
final String company;
Employee(String name, String company) {
super(name); // must be the first statement
this.company = company;
System.out.println("2. Employee constructor: company set");
}
}
static class Manager extends Employee {
final int teamSize;
Manager(String name, String company, int teamSize) {
super(name, company);
this.teamSize = teamSize;
System.out.println("3. Manager constructor: team size set");
}
}
public static void main(String[] args) {
Manager m = new Manager("Asha", "Acme", 8);
System.out.println(m.name + " manages " + m.teamSize + " people at " + m.company);
}
}
1. Person constructor: name set 2. Employee constructor: company set 3. Manager constructor: team size set Asha manages 8 people at Acme
The parent part of the object is always fully initialised before the child's constructor body runs, so the child can safely use inherited fields.
4. Implicit super() Calls
If a constructor does not start with this(...) or super(...), the compiler silently inserts super();, a call to the parent's no-argument constructor. That works only if such a constructor exists:
public class ImplicitSuper {
static class Animal {
Animal() { System.out.println("Animal() runs first, even without an explicit super()"); }
}
static class Dog extends Animal {
Dog() {
// the compiler inserts super(); here
System.out.println("Dog() runs second");
}
}
// If the parent has NO no-argument constructor, the child MUST call super(...) explicitly
static class Vehicle {
final int wheels;
Vehicle(int wheels) { this.wheels = wheels; }
}
static class Bike extends Vehicle {
Bike() { super(2); } // without this line: compile error
}
public static void main(String[] args) {
new Dog();
System.out.println("Bike wheels: " + new Bike().wheels);
}
}
Animal() runs first, even without an explicit super() Dog() runs second Bike wheels: 2
Without the explicit super(2) in Bike, compilation fails because Vehicle has no no-argument constructor:
Bike.java:2: error: constructor Vehicle in class Vehicle cannot be applied to given types;
class Bike extends Vehicle { Bike() { } }
^
required: int
found: no arguments
reason: actual and formal argument lists differ in length
5. Rules of Constructor Chaining
this(...)orsuper(...)must be the first statement in a constructor (Java 25 relaxes this, allowing simple statements before the call that do not usethis).- A constructor can call either
this(...)orsuper(...), never both. - Calls must not form a cycle:
A()callingA(int)callingA()is a compile error ("recursive constructor invocation"). - Every chain of
this(...)calls ends in a constructor that callssuper(...), explicitly or implicitly. - Arguments to
this(...)/super(...)cannot use instance fields or methods of the object being built, because it is not initialised yet (static methods are fine). - Chaining all the way up ends at
Object(), the root of every class.
6. this(...) vs super(...)
| Feature | this(...) | super(...) |
|---|---|---|
| Calls | Another constructor of the same class | A constructor of the parent class |
| Purpose | Reuse code between overloaded constructors | Initialise inherited state |
| Inserted automatically? | No | Yes, super(), if you write neither call |
| Position | First statement | First statement |
7. When Chains Get Long: Builders
Many overloaded constructors calling each other ("telescoping constructors") become hard to read when a class has many optional parameters: new Pizza("large", "thin", true, false, true) says little. Consider a builder (Pizza.builder().size("large").extraCheese().build()) or a record with static factory methods instead.
8. Summary
- Constructor chaining is one constructor calling another with
this(...)orsuper(...). this(...)reuses code between constructors of the same class.super(...)initialises the parent part of the object first; the compiler addssuper()if you do not.- The call must come first, only one call is allowed, and cycles are forbidden.
- Chains run from the most general class down to the most specific one.
Related Tutorials
Frequently Asked Questions (FAQ)
Q1. What is constructor chaining in Java?
Calling one constructor from another, either within the same class using this(...) or from a subclass to its parent using super(...).
Q2. Why must this() or super() be the first statement?
So that the object, and its parent part, are initialised before your code uses them. Java 25 allows some statements before the call, as long as they do not use the object being constructed.
Q3. Can I use both this() and super() in one constructor?
No. Only one of them can appear, and it must come first. If you call this(...), the constructor you delegate to will call super(...).
Q4. What happens if I don't call super() explicitly?
The compiler inserts super(); automatically. If the parent has no no-argument constructor, compilation fails and you must call an existing parent constructor yourself.
Q5. In what order do chained constructors run?
Parent constructors complete before child constructors. Within a class, the constructor reached at the end of a this(...) chain finishes first, then control returns to the earlier ones.
Q6. What is recursive constructor invocation?
A cycle of this(...) calls, such as A() calling A(int) and back. The compiler rejects it because the object could never finish being created.
Q7. Can constructor chaining be done without inheritance?
Yes, with this(...) between overloaded constructors of the same class. Every class still chains implicitly to Object() through super().
Q8. Can I call a method inside this(...) arguments?
Only static methods or expressions that do not use the current object. Instance methods and fields are not available yet.
Q9. What is the benefit of constructor chaining?
Initialisation and validation code is written once, defaults are expressed clearly, and inherited state is always set up before the subclass uses it.
Q10. Is super() called for the Object class too?
Yes. Every chain ends at Object(), the constructor of the root class, which has an empty body.
Q11. Can a private constructor be called with this()?
Yes. Private constructors are visible inside their own class, so other constructors of the class can delegate to them.
Q12. Can a subclass call a private constructor of its parent?
No, unless the subclass is nested in the parent. A class whose only constructors are private cannot be extended from outside.
Q13. Do abstract classes participate in constructor chaining?
Yes. An abstract class's constructor runs through super(...) when a concrete subclass is instantiated.
Q14. What are telescoping constructors?
A series of overloaded constructors where each adds one more parameter and calls the next. They are fine for a few parameters; with many, a builder is easier to use.
Q15. Can this() be used in a regular method?
No. Constructor calls with this(...) are only allowed as the first statement of a constructor. In methods, this refers to the current object, not a constructor.
Q16. Are constructors inherited if I use super()?
No. super(...) calls the parent's constructor, but the subclass does not gain it as its own constructor.
Q17. Do records support constructor chaining?
Yes. Additional record constructors must delegate to the canonical constructor with this(...), for example record Point(int x, int y) { Point() { this(0, 0); } }.
Q18. Do enums support constructor chaining?
Yes, enum constructors can call each other with this(...). They cannot call super(...), because enums implicitly extend java.lang.Enum.
Q19. What is the difference between this and this()?
this refers to the current object (for fields and methods). this(...) calls another constructor of the same class.
Q20. How do I debug the order of constructor calls?
Add print statements or set breakpoints in each constructor, as the examples above do. The call stack in a debugger also shows the chain.