Constructor chaining
Constructor chaining is calling one constructor from another so setup logic lives in a single place. In Java that call is this(...) in the same class or super(...) to the parent.
Detailed Explanation
Overloaded constructors tempt you to copy the same validation into each body. The copies drift. One overload accepts a blank name; another does not. Chaining fixes that: convenience constructors only translate arguments, then hand off to one constructor that owns the rules.
this(...) must be the first statement in the constructor. The language rule exists because the object is not ready for ordinary method calls until the chained constructor has assigned fields. You cannot log, then chain. You cannot assign a field, then chain. Translate arguments, chain, and if you still need extra work, do it after the call returns, in the same body.
Chaining also runs up the inheritance tree. Every constructor, including those in Object, participates. If you write no super(...) or this(...), the compiler inserts super(). That implicit call requires a no-argument constructor on the superclass. When the parent only has Animal(String name), every child constructor must call super(name) explicitly. Forgetting it is a compile error, not a runtime surprise.
Order of work is always the same: the top of the chain runs first. Object finishes, then the next subclass, down to the class you constructed. Field initializers in each class run just before that class's constructor body. This is why a virtual method called from a superclass constructor sees subclass fields still at their defaults: the subclass constructor has not run yet.
You reach for chaining when a type has several legal ways to be created but one legal internal shape. A Duration might accept seconds, or minutes, or a pair of hours and minutes; all three should end in one field holding total seconds. You avoid chaining when the overloads do unrelated jobs. Those are not the same object being built two ways; they are different factories and should be named methods.
The cost of deep chains is readability. Five constructors that only forward integers become a maze. A static factory or a builder is clearer once the argument list grows past three or four. Keep chains short: one canonical constructor, a handful of forwards.
Code Examples
public class Duration {
private final long seconds;
// Canonical constructor: every other path ends here.
public Duration(long seconds) {
if (seconds < 0) {
throw new IllegalArgumentException("seconds must be >= 0");
}
this.seconds = seconds;
}
public Duration(int hours, int minutes) {
this(hours * 3600L + minutes * 60L); // this(...) must be first
}
public static Duration minutes(long minutes) {
return new Duration(minutes * 60L);
}
public long seconds() {
return seconds;
}
public static void main(String[] args) {
System.out.println(new Duration(1, 2).seconds()); // prints: 3720
System.out.println(Duration.minutes(3).seconds()); // prints: 180
}
}class Animal {
final String name;
Animal(String name) {
this.name = name;
}
}
class Dog extends Animal {
final String breed;
Dog(String name, String breed) {
super(name); // parent has no no-arg constructor, so this is required
this.breed = breed;
}
public static void main(String[] args) {
Dog d = new Dog("Nala", "indie");
System.out.println(d.name + " " + d.breed); // prints: Nala indie
}
}Common Gotchas
`this(...)` and `super(...)` cannot both appear
a constructor chains to exactly one other constructor. Pick same-class or superclass, not both.
Missing `super(...)` when the parent has no default constructor
the implicit `super()` does not compile. Write the call with the required arguments.
Do not call overridable methods from a chained constructor
the subclass fields are still defaults, so the override reads garbage or `null`.
Records chain to the canonical constructor
a compact constructor still ends up assigning every component.