Reputation: 133
I have the following code:
public Account() { #1
id = 0;
balance = 0;
interestRate = 4.5;
}
public Account(int newId, double newBalance) {
//this(); - option 1
//new Account(); - - option 2
id = newId;
balance = newBalance;
}
public Account(int newId) { #2
new Account();
id = novoId;
}
When creating an Object with Account account = new Account(int);
it runs through #2 to #1 to #2.
The question is that option 1 and option 2 seem to render the same behavior (looking through debugger) which seems a bit counter-intuitive. Can somebody shed some light on it?
Upvotes: 0
Views: 246
Reputation: 29282
Looking at your code, your intent seems to be that when you create an instance of Account
class
Account account = new Account(int);
you want to initialize the instance using the no-argument constructor in Account
class and the correct way to do it is using the first option, i.e. this()
.
Reason why first option is correct way to do what you want is because
this();
this will invoke the no-argument constructor to continue the initialization of the newly created instance but
new Account();
will create a new instance of Account
class which will obviously lead to invocation of the no-argument constructor which is probably not what you want.
So, first option will invoke the no-argument constructor to initialize the newly created instance of Account
class whereas the second option will create a new instance of Account
class that will be completely different from the instance that lead to the invocation of the constructor in the first place.
Upvotes: 1
Reputation: 580
Both methods call the default constructor.
The first option is correct, because I suppose you want to set all the variables of the current instance.
Upvotes: 1
Reputation: 108959
The difference is very important: this()
will chain the creation/initialization of the current object to the no-argument constructor Account()
, while new Account()
will create a second object of type Account
using the no-argument constructor Account()
, and then immediately throw it away.
For example, with:
public Account(int newId) {
new Account();
id = novoId;
}
Calling, new Account(5)
, will do
End result: created object has id=5, balance=0 (default), interestRate=0 (default)
With
public Account(int newId) {
this();
id = novoId;
}
Calling, new Account(5)
, will do
End result: created object has id=5, balance=0, interestRate=4.5
So, an important difference is that when you use this()
in your constructor, the interestRate
of the object you created with new Account(int)
or new Account(int, double)
will have been initialized to 4.5, but when using new Account()
in your constructor, it will be 0 (assuming you don't have an initialization in the declaration itself).
Personally, I would recommend changing your constructors to :
public Account(int newId, double newBalance) {
id = newId;
balance = newBalance;
interestRate = 4.5;
}
public Account() {
this(0, 0);
}
public Account(int newId) {
this(newId, 0);
}
That is - in my opinion - more clear, and establish a clear order of initialization.
Upvotes: 1