Constructor Chaining with this() in Java
Fast recall
Quick revision
Core answer
this(args) call must be the first statement, and it routes every constructor through a single 'primary' constructor so defaults and validation aren't duplicated. (super() chains to the parent; this() chains within the class.)Whether you understand this() delegates within the same class, must be first, and centralizes initialization.
Putting statements before this(), or confusing this() (same class) with super() (parent class).
Note you can't use both this() and super() in one constructor, and explain why this() must be first (the object must be fully initialized once).
Interview-ready guide
Interview answer
I'd start with the picture: this(...) lets one constructor call another constructor in the same class, so the lighter constructors just fill in defaults and hand off, while one fuller constructor does the real field assignment and validation. That fuller one becomes your single source of truth — change the setup logic once and every constructor that delegates to it benefits, with no copies to drift apart.
There's a strict rule that catches people: this(...) has to be the very first statement. You can't log, declare a variable, or do anything before it. Java insists the object be initialized in one clean, predictable sequence and won't run code against a half-built object. Any extra logic goes after the this(...) call, not before.
The distinction worth drawing out is this() versus super(). this() delegates sideways, to another constructor in the same class; super() reaches up to the parent. Both have to be the first statement, which is exactly why you can't have both in one constructor — there's only one first line. So you pick one, and typically the primary constructor at the end of the chain is the one that calls super().
The trap I'd flag is circular chaining: if A delegates to B and B delegates back to A, the compiler catches the loop and refuses. The chain always has to bottom out at a constructor that doesn't call this(...).
Constructor Chaining with this() in Java
Direct Answer
Constructor chaining with this() is when one constructor calls another constructor in the same class to reuse its initialization. The this(args) call must be the first statement, and it routes every constructor through a single 'primary' constructor so defaults and validation aren't duplicated. (super() chains to the parent; this() chains within the class.)
Code Example
class Rectangle {
int width;
int height;
Rectangle() { // convenience: 1x1 default
this(1, 1); // MUST be the first statement
}
Rectangle(int side) { // convenience: square
this(side, side); // delegates to primary
}
Rectangle(int width, int height) { // PRIMARY CONSTRUCTOR
if (width <= 0 || height <= 0)
throw new IllegalArgumentException("dimensions must be positive");
this.width = width; // field assignment in ONE place
this.height = height;
}
}
Rectangle r1 = new Rectangle(); // width=1, height=1
Rectangle r2 = new Rectangle(5); // width=5, height=5 (square)
Rectangle r3 = new Rectangle(4, 6); // width=4, height=6
Common Mistakes & Pitfalls
- Writing code before
this(...)— the delegation must be the literal first statement; even a single log line ahead of it is a compile error. - Using both
this()andsuper()in one constructor — each must be the first statement, so they're mutually exclusive; let the delegated constructor callsuper(). - Creating a circular chain — if A calls
this()to B and B callsthis()back to A, the compiler rejects it; the chain must end at a constructor that doesn't callthis(...). - Confusing
this()withsuper()—this()delegates sideways within the same class,super()reaches up to the parent; mixing them up changes which constructor actually runs.
Check your understanding
Answer these without looking back at the article.
- What happens if you put this() as the second statement in a constructor?
- What is the difference between this() and super() in constructors?
- Can you call this() from a method (not a constructor)?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.