Java private field access possible when having a reference?

前端 未结 7 1379
野性不改
野性不改 2020-12-19 03:12

I came across the following \"strange\" feature today - if you have a reference to an object from the class A in the body of the class A you can access the private fields of

7条回答
  •  既然无缘
    2020-12-19 03:32

    It makes sense if you consider the intention of the 'private' modifier to hide implementation details.

    Try thinking of it in terms of "this should be private to this class" (which in Java equates to "this should be private to this source file") rather than "this should be private to this instance".

提交回复
热议问题