4

For instance, lets say that we have an object called car which is a part of Cars class and it has a color property. And assuming that we have a getter for the color such that:

public String getColor(){
  return color;
}

But should not it be this.color? If not, why?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Huzo
  • 1,652
  • 1
  • 21
  • 52

3 Answers3

5

You use this implicitly here. When you "leave" out the object on which you "access" a field or call a method ... then that is the same as saying this.field or this.foo() Unless of course, that the name you are using refers to a local variable for example. So, just to be precise: when you have code such as

void setter(Whatever foo) {
  this.foo = foo;

then of course you have to use this in order to differentiate between the field foo and the local variable foo that is shadowing that field.

Any slightly experienced Java programmer knows that. Therefore it is good practice to not write down this here. Keep in mind: you write your code so that your human readers understand what is going on. The compiler and IDEs are fine with using this ... or not using this. But for your human readers it simply means a little bit less of information to process when you leave out this keyword here.

That is all there is to this.

GhostCat
  • 137,827
  • 25
  • 176
  • 248
  • 1
    You forgot to state that this is implicite because there is no other variable in that scope with the same name. The perfect example is a setter. – AxelH Sep 06 '17 at 10:34
  • 1
    @AxelH Very valid point. Updated accordingly. – GhostCat Sep 06 '17 at 10:37
2

'this' always represents current object. SO if you say this.color it's excatly same as simply say 'color'. You can doesn't mean you should :)

Vijayanath Viswanathan
  • 8,027
  • 3
  • 25
  • 43
  • Since you mention the `current object`, it is interesting to note that this is the object defining the method, for nested class, this become a bit more complicated ;) – AxelH Sep 06 '17 at 11:13
1

It's superfluous. In general you use the this.something only if you have a parameter in the method's signature with the same name. Classical example are constructors:

public MyClass(String val1, String val2) {
    this.val1 = val1;
    this.val2 = val2;
}

It was used more often in the past when there was no syntax highlighting in IDEs that included the different presentation of member and local variables. Then this was used to make it easier to distinguish between these two types of variables.

Lothar
  • 5,323
  • 1
  • 11
  • 27
  • Ahh okay. Thanks! – Huzo Sep 06 '17 at 10:22
  • 1
    "_Then this was used to make it easier to distinguish between these two types of variables._" If you remove `this.` in your example, the result won't be the same at all. That's not just to be readable for you, but also for the compiler to know what variable to use. – AxelH Sep 06 '17 at 10:37
  • @AxelH In my second paragraph I was explaining why `this` was used more often in situations where it wasn't needed. It has nothing to do with the previous example of code where `this` is required. – Lothar Sep 06 '17 at 10:57
  • But your reasons are _before syntax highlighting_ and _easier to distinguish_. There is a real need for `this`, that you said in the first part. So that second part kind of say the opposite, so this is a good and bad answer ^^ – AxelH Sep 06 '17 at 11:11