3

I have set my constructor like this:

public class VendingMachine {

    private double currentBalance;
    private double itemPrice;
    private double totalCollected;

    public VendingMachine(double itemCost) {
        currentBalance = 0;
        totalCollected = 0;
        itemPrice = itemCost;
    }
    ...
}

My question is what is the difference from setting up my constructor like above by taking in an argument of a double itemCost.

What is the difference as opposed to making it:

this.itemPrice = itemCost;
Aboutblank
  • 697
  • 3
  • 14
  • 31
user2318861
  • 133
  • 1
  • 1
  • 6

5 Answers5

5

In your case there will be no difference. The this component is sometimes required if we want to differentiate the constructor argument from the class field:

public VendingMachine(double itemPrice) {    // notice the name change here
    itemPrice = itemPrice;         // has no effect
    this.itemPrice = itemPrice;    // correct way to do it
}

From the JLS §6.4.1:

The keyword this can also be used to access a shadowed field x, using the form this.x. Indeed, this idiom typically appears in constructors (§8.8):

  
class Pair {
    Object first, second;
    public Pair(Object first, Object second) {
        this.first = first;
        this.second = second;
    }
}

Here, the constructor takes parameters having the same names as the fields to be initialized. This is simpler than having to invent different names for the parameters and is not too confusing in this stylized context. In general, however, it is considered poor style to have local variables with the same names as fields.

arshajii
  • 127,459
  • 24
  • 238
  • 287
1

It this case it's really doesn't matter. It would make a difference if your parameters had the same name like attribute

public VendingMachine(double itemPrice) {
   this.itemPrice = itemPrice;
}

Then you would need to distinguish which one is the class member and which one is method scope variable. So this is what this keyword serves for (you can also use it to call other class constructors and so on).

But the convention is to give the class attributes and method parameters the same and use the this keyword to prevent confusion.

Petr Mensik
  • 26,874
  • 17
  • 90
  • 115
1

Here it is ok to assign the way you did, but generally in Java we follow proper naming conventions where we usually keep names such that there is no misunderstanding like in your case...

public VendingMachine(double itemPrice) {
currentBalance = 0;
totalCollected = 0;
this.itemPrice = itemPrice;

here 'this' refers to the object by which method got called, so ultimately the received itemPrice will be assigned to the object state(variable) itemPrice.

dev2d
  • 4,245
  • 3
  • 31
  • 54
0
this.itemPrice = itemCost;

There is no actual difference in your example. itemPrice is already in the scope of the method. The difference comes when there is another variable declared itemPrice in the method. To differentiate between the member variable and local variable, we use this.

Mahesh
  • 34,573
  • 20
  • 89
  • 115
0

Please check this link for more calrity on usage of this. But in your case as others commented it just helps code more readable if name of variable in constructor args is same as instance variables of class.

Java - when to use 'this' keyword and http://docs.oracle.com/javase/tutorial/java/javaOO/thiskey.html

Community
  • 1
  • 1
Harish Kumar
  • 528
  • 2
  • 15