Constructor chaining with this() in Java
Calling another constructor; must be the first statement (before Java 25 flexible bodies).
this(...).Delegate, don't duplicate
A constructor can call another constructor of the same class with this(...). Simple constructors fill in defaults and hand over to the "full" one, so the setup logic lives in one place.
class Pizza {
String size;
Pizza() { this("M"); }
Pizza(String size) {
this.size = size;
}
}Who finishes first?
new Box() triggers a chain. What does it print?
class Box {
Box() { this(1); p("X"); }
Box(int a) { this(a, 2); p("Y"); }
Box(int a, int b) { p("Z"); }
void p(String s) { System.out.print(s); }
}
void main() { new Box(); }XYZZYXXXZY
Show the answer
ZYX. Box() calls this(1), which calls this(a, 2). The innermost body runs first (Z), then control returns to finish Y, then X.
Bodies finish in reverse
It works like nested method calls: the chained call goes in first, and each constructor's remaining body runs on the way back out. Last called, first finished.
Java 25: code before this(...)
Before Java 25, this(...) had to be the very first statement. Java 25's *flexible constructor bodies* (JEP 513) allow statements before it — perfect for checking arguments — as long as they don't use the object being built.
class Temp {
int c;
Temp(int c) {
if (c < -273)
throw new IllegalArgumentException();
this(c, true);
}
Temp(int c, boolean ok) { this.c = c; }
}No peeking at the object
Code before this(...) runs while the object is not yet initialized, so it may not read fields or call instance methods — even in Java 25. Only parameters, locals and static stuff are allowed.
class A {
int x = 5;
A() {
System.out.println(x); // error!
this(1);
}
A(int y) { }
}this(...) and super(...)?
Can one constructor call this(...) and then super(...)?
Think about it, then reveal the answer
No. A constructor makes at most one explicit constructor call. If it delegates with this(...), the constructor at the end of the chain is the one that calls super(...).
In real projects
Library classes overflow with chained constructors — "telescoping constructors" with sensible defaults. Before Java 25, validating arguments before delegating needed awkward static helper tricks like this(check(c)); flexible bodies make that plain code.
Key takeaways
- this(...) reuses another constructor of the same class
- Only one this(...) or super(...) call per constructor
- Java 25: code may run before this(...) if it doesn't read this
- Bodies finish in reverse order of the calls
Flexible constructor bodies took three preview rounds — Java 22, 23 and 24 — before becoming final in Java 25 as JEP 513.
Practice questions
What does this print?
class Pizza {
Pizza() { this("M"); p("A"); }
Pizza(String s) { this(s, 8); p("B"); }
Pizza(String s, int n) { p("C"); }
void p(String x) { System.out.print(x); }
}
void main() { new Pizza(); }- ABC
- CBA
- A
- ACB
Check your answer
CBA. new Pizza() calls this("M"), which calls this(s, 8). The innermost body prints C first, then control returns to print B, then A.
What does this print?
class Temp {
int c;
Temp(int c) {
if (c < -273)
throw new IllegalArgumentException();
this(c, true);
}
Temp(int c, boolean ok) { this.c = c; }
}
void main() { System.out.println(new Temp(20).c); }- 20
- 0
- Compile error: this() must come first
Check your answer
20. Since Java 25, flexible constructor bodies allow statements before this(...) as long as they don't use the object under construction. Validating an argument is the classic use.