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

前端 未结 16 2203
广开言路
广开言路 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:33

    I think there's nothing wrong if you put in this before the property name. This helps disambiguate the property from local variables should you have any declared with the same name (which may happen in a constructor when you initialize properties).

    It doesn't affect performance, it doesn't create any problems for readability (if at all it makes reading easier).

    So I don't think we need to worry about this. Let the programmers make their own choice.

提交回复
热议问题