Reputation: 11334
I've never needed to do this before but since both have to be the 'first' line in the constructor how should one tackle it? What's the best refactoring for a situation like this?
Here's a sample:
public class Agreement extends Postable {
public Agreement(User user, Data dataCovered)
{
super(user);
this(user,dataCovered,null);
}
public Agreement(User user,Data dataCovered, Price price)
{
super(user);
if(price!=null)
this.price = price;
this.dataCovered = dataCovered;
}
...
}
The call to super(user)
is an absolute must. How to deal with "optional parameters" in this case? The only way I can think of is repetition i.e., don't call this(...) at all. Just perform assignments in every constructor.
Upvotes: 16
Views: 4480
Reputation: 5459
In constructors there are (among others like calling not instance methods from or within super or this) those three restrictions:
super(...)
or this(...)
per constructorsuper
or this
always have to be the first statement in the methodsuper()
at the beginning of the constructorThe reason for this is that an object can only be created once. And super
is creating an object by calling the overloaded constructor while this is delegating to another constructor of the same class.
As another answer is saying in your case you do not need the first super
as your this
statement is delegating to the other constructor which is already calling super(user)
.
Upvotes: 0
Reputation: 10588
I would separate out the logic you need to perform in the constructor in a static method in the class, and just call it froum both constructors, to avoid duplication. However, in your case, you can just skip the call to super(user)
from the first constructor, and the second one will call it for you :). And I would reverse the "dependencies" between the constructors, like this:
public class Agreement extends Postable {
public Agreement(User user, Data dataCovered)
{
super(user);
setDataCovered(dataCovered);
}
public Agreement(User user, Data dataCovered, Price price)
{
this(user, dataCovered);
if(price!=null)
setPrice(price);
}
private static void setDataCovered(Data dataCovered) {
this.dataCovered = dataCovered;
}
private staitc void setPrice(Price price) {
this.price = price;
}
}
Upvotes: 0
Reputation: 8245
You cannot call both super(..) and this(...). What you can do is re-work the structure of your overloadeded constructors, so that the last one to be called will call super(...). If that is not an option, you'll have to do the assignments in every constructor.
Upvotes: 22
Reputation: 691765
If you call this(user,dataCovered,null)
, the second constructor will be called, and the first thing it will do is call the super constructor. So the line super(user);
in the first constructor is unnecessary.
Upvotes: 8