0

I have came across how different ways how people access instance variables in the class. For example:

public int getValue() {return value;}

public int getValue() {return this.value;}

Is there any performance benefit of using this.value instead of just value. I understand that using this. ensures that there are no namespace conflicts but is there any other added benefit?

Aman Gupta
  • 3,627
  • 4
  • 40
  • 64

1 Answers1

0

There is zero performance benefit. The two things are exactly the same. The difference is purely syntax. I prefer to use "this" myself because modern IDEs catch the problems that this can create, I like to be able to use the same name for a local variable and a field (color coding in IDEs makes this even clearer) and I don't like using some kind of prefix for member variables. I'd say this is pretty stylistic though and varies from person to person. To be kindof "cute", I also like using "that" as the name of variables at times when the two references of the same type that are going to be compared or copied or something. This and that are English words and "_" or "m_" or the like is not. That's the main difference in my mind. I like my code to be English-like and I prefer to avoid abbreviations. The easier it is for the uninitiated to read the code, the better. People are always coming and going from teams, so being clear is very important, not just in this small case, but across the board.

Jonathan Locke
  • 243
  • 2
  • 6