-5

Is there any performance advantage for referring to an instance variable foo as this.foo as opposed to simply foo?

MC Emperor
  • 22,334
  • 15
  • 80
  • 130
Gengar
  • 123
  • 2
  • 8
  • 4
    Don't you think that if there was a difference, the compiler would resolve by itself? – Mephy Jun 13 '14 at 17:04
  • 1
    It slows down the compiler by a few nanoseconds and the programmer by up to a few seconds, depending on typing speed. Other than that: NO. – maaartinus Jun 13 '14 at 17:20

1 Answers1

2

There is no difference in terms of compiled bytecode or performance1.

Using the this.foo form is useful when there is a local shadowing foo in scope2 - in which case both forms do something different, so the above statement doesn't apply - but it otherwise has no effect.


1 See Does using the 'this' keyword affect Java performance?, which shows that the generated bytecode is identical; and closing for a duplicate!

2 See Java - when to use 'this' keyword and When to use "this" in Java for examples and in-depth explanations.

Community
  • 1
  • 1
user2864740
  • 60,010
  • 15
  • 145
  • 220