Reputation: 57
I want to know whether I need to initialize(call) all the constructors when I have overloaded constructors in Java.
For instance,
public class Dog {
int size = 0;
public Dog() {
size = 20;
}
public Dog(int size) {
this.size = size;
}
}
edited:
I understand that when the user calls the constructor without any parameter, public Dog() will be called. Will there be any compile error if I do not initialize all the constructors in a class?
When I create Dog d = new Dog(); it will automatically call public Dog(). But, do I also need to declare Dog d = new Dog(40) ?
I think that it wouldn't make sense if I need to initialize both Dog constructors, but I was wondering if there will be problems in other examples.
Upvotes: 1
Views: 178
Reputation: 10084
Overloading constructors should be thought of this way: it provides you with a way to construct the same object from different input sets. Here is an important rule about constructors:
When overloading a constructor, it is important that the parameters passed to each constructor be sufficient to completely specify and construct the target object (as required by its contract).
Calling a constructor always returns an object as long as there were no exceptions. You cannot invoke a second constructor on an object that has already been instantiated ... because it has already been constructed.
As an example, consider the following class:
public class Pair {
private final int x
private final int y
public Pair() {
this(0, 0);
}
public Pair(int x, int y) {
this.x = x;
this.y = y;
}
public Pair(MyDoublet dbl) {
this.x = dbl.getX();
this.y = dbl.getY();
}
:
:
}
There are three constructors. Invoking any of them yields a completely specified Pair object. In fact, if a constructor didn't completely specify the Pair object the compiler would issue an error because, for the Pair class, all the instance fields are final and must be specified in the constructor.
Pair p1 = new Pair(); // p1 has x=0, y=0
Pair p2 = new Pair(1, 3); // p2 has x=1, y=3
Pair p3 = new Pair(0, 0); // p3 is the same has p1 even though a
// different constructor was used.
TLDR: Overloading constructors allows you to specify the construction of objects in the same class but with different sets of inputs.
Upvotes: 0
Reputation: 718826
I wanted to know whether I need to call all the constructors in a class.
Will there be any compile error if I do not initialize all the constructors in a class?
No.
You don't have to call all of the constructors. You won't get a compilation error1 if you write a constructor, or a method and then don't use it anywhere in your application.
You don't even have to call any of the constructors ... if you don't want any instances of the class. (And there are Java coding patterns where a particular class does not need to be instantiated ... ever.)
1 - However, an automated style checker / bug checker, or someone marking / reviewing your code could well notice redundant constructors and methods and "ping" you for it.
Upvotes: 1