What if you could write your constructors once and reuse them everywhere without repeating yourself?
Why Constructor chaining in Java? - Purpose & Use Cases
Start learning this pattern below
Jump into concepts and practice - no test required
Imagine you are building a class with many constructors to create objects in different ways. You write similar code in each constructor to set default values or initialize fields. This means repeating yourself a lot.
Writing similar code in every constructor is slow and tiring. It's easy to make mistakes or forget to update all constructors when something changes. This repetition makes your code messy and hard to maintain.
Constructor chaining lets one constructor call another in the same class. This way, you write common initialization code once and reuse it. It keeps your code clean, reduces errors, and makes updates easier.
public class Car { private String color; private int year; public Car() { this.color = "red"; this.year = 2020; } public Car(String color) { this.color = color; this.year = 2020; } public Car(String color, int year) { this.color = color; this.year = year; } }
public class Car { private String color; private int year; public Car() { this("red", 2020); } public Car(String color) { this(color, 2020); } public Car(String color, int year) { this.color = color; this.year = year; } }
Constructor chaining enables writing cleaner, easier-to-maintain code by reusing initialization logic across multiple constructors.
Think of a smartphone app where you create user profiles. Some profiles have just a name, others have name and age, and some have name, age, and location. Constructor chaining helps set default values and avoid repeating code for each profile type.
Constructor chaining reduces repeated code in multiple constructors.
It makes your code cleaner and easier to update.
It helps avoid mistakes when initializing objects in different ways.
Practice
Solution
Step 1: Understand constructor chaining concept
Constructor chaining means one constructor calls another constructor in the same class to reuse code.Step 2: Identify the correct purpose
To allow one constructor to call another constructor in the same class correctly describes this behavior usingthis(...)to call another constructor.Final Answer:
To allow one constructor to call another constructor in the same class -> Option AQuick Check:
Constructor chaining = calling another constructor [OK]
- Confusing constructor chaining with inheritance
- Thinking constructors can have different names
- Believing constructor chaining creates multiple objects
Solution
Step 1: Recall syntax for constructor chaining
Constructor chaining usesthis(...)as the first statement inside a constructor.Step 2: Identify correct option
this(); // must be the first statement in constructor showsthis();which is the correct syntax to call another constructor in the same class.Final Answer:
this(); // must be the first statement in constructor -> Option CQuick Check:
Constructor chaining syntax = this() first [OK]
- Using super() to call same class constructor
- Calling constructor by its name directly
- Placing this() after other statements
class Test {
Test() {
this(5);
System.out.print("A");
}
Test(int x) {
System.out.print("B");
}
public static void main(String[] args) {
new Test();
}
}Solution
Step 1: Trace constructor calls
Creating new Test() calls the no-arg constructor, which calls Test(int x) first (prints "B"), then prints "A".Step 2: Determine output order
Since Test(int x) prints "B" first, then control returns to no-arg constructor which prints "A", output is "BA".Final Answer:
BA -> Option BQuick Check:
Constructor chaining prints B then A [OK]
- Assuming outer constructor prints before inner
- Confusing order of constructor calls
- Ignoring that this() must be first line
class Sample {
Sample() {
System.out.println("Hello");
this(10);
}
Sample(int x) {
System.out.println(x);
}
}Solution
Step 1: Check constructor chaining rules
In Java, the call to another constructor usingthis(...)must be the first statement in the constructor.Step 2: Identify the error
Here,System.out.println("Hello")comes beforethis(10);, which violates the rule.Final Answer:
Constructor chaining call this(10) must be the first statement -> Option AQuick Check:
this() must be first line in constructor [OK]
- Placing code before this() call
- Confusing constructor overloading with chaining
- Thinking constructors need return types
new Box(); is executed?class Box {
Box() {
this(3, 4);
System.out.print("X");
}
Box(int w, int h) {
this(w, h, 5);
System.out.print("Y");
}
Box(int w, int h, int d) {
System.out.print("Z");
}
}Solution
Step 1: Trace constructor chaining calls
Callingnew Box()calls no-arg constructor, which callsBox(int w, int h), which callsBox(int w, int h, int d).Step 2: Track printed characters in order
The deepest constructor prints "Z" first, then returns to middle constructor which prints "Y", then returns to no-arg constructor which prints "X".Final Answer:
ZYX -> Option DQuick Check:
Chained constructors print Z then Y then X [OK]
- Assuming outer constructor prints first
- Ignoring chaining order
- Mixing up print order in nested calls
