In Java, why do people prepend fields with `this`?

前端 未结 16 1999
广开言路
广开言路 2020-12-20 11:18

When referencing class variables, why do people prepend it with this? I\'m not talking about the case when this is used to disambiguate from method

相关标签:
16条回答
  • 2020-12-20 11:48

    I most often see people do this because it triggers the intellisense. I personally prefer to leave off the "this." because it creates more code without any value.

    0 讨论(0)
  • 2020-12-20 11:49
    1. Defensive programming (in case someone editing the code later adds a parameter or local with a conflicting name
    2. Make the code "self documenting," more obvious
    0 讨论(0)
  • 2020-12-20 11:55

    They are perhaps a Python programmer and get tortured/lost/confused without an explicit this.

    0 讨论(0)
  • 2020-12-20 11:55

    "this" prevents confusion with instance variables with the same name in the parent class/es.

    It's pretty much the complement to prepending with "super".

    0 讨论(0)
提交回复
热议问题