1

I am looking for some guidance on the this keyword. I just learned about the concept and it is sort of confusing to me. Here is a piece of my code:

public class CashRegister {
    private ArrayList<Double> items;
    private int itemCount;
    private double totalPrice;

        public double getTotal() {
            this.totalPrice = 0;
            for (int i = 0; i < this.items.size(); i++) {
                this.totalPrice = this.totalPrice + this.items.get(i);
            }
            return totalPrice;
        }
    }

My question is if am I using 'this' right? Should I use 'this' every time I use a variable like totalPrice or the items ArrayList?

WizKid22
  • 23
  • 2
  • 5
  • 1
    I wouldn't. It's redundant and anyone who understand the language knows that `totalPrice` refers to the instance field. – markspace Dec 02 '17 at 20:34
  • In this case, they are not necessary. The keyword is used when there is a possibility of a conflict. Here, the only object being referred to in getTotal() is the object used to call it. Hence, no conflicts in references as to which object's attributes do I work with. – Sayan Sil Dec 02 '17 at 20:39
  • I like it. For me it's one way of preventing conflicts. Another way is to not create name conflicts at all. For example by giving your members a prefix like `mTotalPrice` (like `_totalPrice` in other languages). If you work with an IDE you can probably set some advanced rule such that it warns you if you ever forgot to use `this` or if you create such a name conflict (at least Eclipse has this). – Zabuzard Dec 02 '17 at 20:42
  • Why store `totalPrice` as a field if you actually calculate the total in the accessor method? – Elliott Frisch Dec 02 '17 at 20:51

3 Answers3

2

It's not wrong. But it's polluting the code because it's redundant. We all know that items and totalPrice are fields of the current object (referenced by this).

Here is a piece of code in which this makes sense:

public class Test {
    private String test;

    public Test(String test) {
        this.test = test;
    }
}

You may see that the reference to test is ambiguous. Inside the constructor if you just use test you won't know if you are accessing the field of the object of type Test or the reference String that was passed to the constructor, in this case you will be accessing the reference. So this is used to make sure you're using the field of the object of type Test.

There is a concept called shadowing. Using this is helpful to not fall in this trap of thinking that you're referencing something when you aren't.

this question is helpful: What is variable shadowing used for in a Java class?

wleao
  • 2,316
  • 1
  • 18
  • 17
  • @WizKid22 Actually, it's the definition of the class Test. If you instantiate it like Test t1 = new Test("abc"). Then t1 will be an object of type Test. Think of a class as a blueprint and the object as the actual building. – wleao Dec 02 '17 at 21:02
1

The way you use this is definitely not wrong, but is redundant. You could write the same code without the thises and get the same result.

Mureinik
  • 297,002
  • 52
  • 306
  • 350
1

Generally, the most common usage of "this" is in object constructors. So you could have some thing like...

public NewThing(int count, double price) {
    this.count = count;
    this.price = price;
}

So it's mostly used to identify the object with which you're assigning the arguments that are being passed in. Because the variable names are the same as the arguments, "this" signals that it is "this object" receiving those attributes.

slickset
  • 179
  • 13