且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在Java嵌套类中,封闭类是否可以访问内部类的私有成员?

更新时间:2021-08-27 23:01:23

是, 没关系。从JLS,第6.6节。 1

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


否则,如果声明成员或构造函数 private ,然后允许访问,当且仅当它发生在包含成员或构造函数声明的***类(第7.6节)的主体内时。

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.

你甚至可以在另一个嵌套类型Y中引用嵌套类型X的私有成员,只要它们共享一个***类。

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.