Constructor chaining in Java - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
Constructor chaining means one constructor calls another in the same class. We want to see how this affects the time it takes to create an object.
How does the number of constructor calls grow when chaining happens?
Analyze the time complexity of the following code snippet.
public class Box {
int width, height, depth;
public Box() {
this(1, 1, 1); // calls the 3-parameter constructor
}
public Box(int w, int h, int d) {
width = w;
height = h;
depth = d;
}
}
This code shows one constructor calling another to set default values.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Constructor call chaining (one constructor calls another exactly once)
- How many times: Each constructor calls another only once, so the chain length is fixed and small.
Execution grows by the number of constructor calls in the chain, which is fixed here.
| Input Size (n) | Approx. Operations |
|---|---|
| 1 (one object) | 2 constructor calls |
| 10 (ten objects) | 20 constructor calls |
| 100 (hundred objects) | 200 constructor calls |
Pattern observation: The number of constructor calls grows linearly with the number of objects created, but each object creation involves a fixed small chain of calls.
Time Complexity: O(n)
This means creating n objects with constructor chaining takes time proportional to n, since each object requires a fixed number of constructor calls.
[X] Wrong: "Constructor chaining makes object creation take much longer because it repeats many times."
[OK] Correct: The chain length is fixed and small, so it adds only a constant extra cost per object, not a growing cost.
Understanding constructor chaining helps you explain object creation clearly and shows you know how code structure affects performance in simple ways.
"What if the constructor chain was longer or recursive? How would the time complexity change?"
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
