In Java nested classes, can the enclosing class access private members of inner classes?

后端 未结 1 1924
野趣味
野趣味 2020-12-09 09:53

In Java, the inner class can access private members of enclosing class. But can the outer class access private members of inner class? This is irrespective of whether inner

相关标签:
1条回答
  • 2020-12-09 10:26

    Yes, that's fine. From the JLS, section 6.6.1:

    Otherwise, if the member or constructor is declared private, then access is permitted if and only if it occurs within the body of the top level class (§7.6) that encloses the declaration of the member or constructor.

    You can even refer to a private member of nested type X within another nested type Y so long as they share a top-level class.

    At the bytecode level, I believe this is all implemented by adding synthetic package-access methods.

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