Igor Kolesnikov
Igor Kolesnikov

Reputation: 133

What is the difference between this() and new Object() inside a overloaded constructor?

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

Answers (3)

Yousaf
Yousaf

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

MauroB
MauroB

Reputation: 580

Both methods call the default constructor.

  • Using "this()" you are calling the constructor of the current instance
  • with "new Account()" you are creating a new instance by calling its default constructor.

The first option is correct, because I suppose you want to set all the variables of the current instance.

Upvotes: 1

Mark Rotteveel
Mark Rotteveel

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

  1. Create object#1 of type Account
  2. Call constructor Account(int) with 5 for object#1
  3. Create object#2 of type Account
  4. Call constructor Account() for object#2
  5. Set id=0, balance=0, interestRate=4.5 for object#2
  6. (end constructor of step 4)
  7. (return to constructor of step 2)
  8. Set id=5 for object#1
  9. (end constructor call of step 2)

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

  1. Create object#1 of type Account
  2. Call constructor Account(int) with 5 for object#1
  3. Call constructor Account() for object#1
  4. Set id=0, balance=0, interestRate=4.5 for object#1
  5. (end constructor of step 3)
  6. (return to constructor of step 2)
  7. Set id=5 for object#1
  8. (end constructor call of step 2)

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

Related Questions