Sub topic 6
Overloading constructors and delegating with this
A constructor is code that runs when you create an object with new. A class can have more than one constructor, so you can offer different ways to set up an object.
Constructor overloading means defining constructors in the same class with different parameter lists. A parameter is a named input in a constructor definition. The constructors can differ in the number of parameters, their types, or the order of their types.
Two ways to create a counter
The following program creates two counters. One starts at zero. The other starts at a number chosen by the caller.
Save this code in a file named Counter.java:
class Counter {
private int value;
Counter() {
this(0);
}
Counter(int initialValue) {
this.value = initialValue;
}
void increment() {
value++;
}
int getValue() {
return value;
}
public static void main(String[] args) {
Counter first = new Counter();
Counter second = new Counter(5);
first.increment();
second.increment();
System.out.println(first.getValue());
System.out.println(second.getValue());
}
}Compile and run it:
javac Counter.java
java CounterThe program prints 1 and then 6.
The class has two constructors:
Counter()takes no inputs. It starts the counter at zero.Counter(int initialValue)takes one integer. It starts the counter at that value.
An argument is a value passed to a constructor. In new Counter(5), the argument is 5. Java selects the constructor that takes an int, and its initialValue parameter receives 5.
In new Counter(), there is no argument, so Java selects the constructor with no parameters. A constructor with no parameters is also called a no-argument constructor.
How delegation works
Constructor delegation means that one constructor calls another constructor in the same class. In the example, the no-argument constructor delegates with this line:
this(0);This calls Counter(int initialValue) with 0. Both constructors work on the same object. The call does not create a second counter.
The steps for new Counter() are:
- Java starts the no-argument constructor.
this(0)calls the constructor that takes an integer.- The integer constructor sets
valueto0. - Control returns to the no-argument constructor, which then finishes.
Delegation lets you keep shared setup code in one place. Without it, both constructors would need their own assignment to value.
The word this has a related use in the assignment:
this.value = initialValue;Here, this means the current object, and this.value is its field. A field is a variable that belongs to an object. The field is private, which means code outside the class cannot access it directly.
Rules and common mistakes
- A constructor has the same name as its class and has no return type, not even
void. - Changing only a parameter name does not make a new overload. For example,
Counter(int start)andCounter(int initialValue)have the same parameter type. - A chain of
this(...)calls must not loop back to a constructor already in the chain. Java rejects such a loop. - If you write any constructor, Java does not supply a no-argument constructor for you. Write one yourself if callers need
new Counter().
Check your understanding
Exercise
What starting value does new Counter() give the counter in the example, and which constructor sets it?
Show answer
It starts at 0. The no-argument constructor calls Counter(int initialValue) with 0, and that constructor sets the field.
Exercise
Can you add Counter(int start) alongside Counter(int initialValue) as another overload?
Show answer
No. Both have one parameter of type int. A different parameter name alone does not create a different overload.