简体   繁体   中英

Static nested class has access to private constructor of outer class

It is stated that:

A static nested class interacts with the instance members of its outer class (and other classes) just like any other top-level class. In effect, a static nested class is behaviorally a top-level class that has been nested in another top-level class for packaging convenience.

So how to explain this static nested class has access to private constructor of its enclosing class?

So how to explain this static nested class has access to private constructor of its enclosing class?

Because the scope and accessibility of any private member (field, constructor or method) is the complete body of its declaring class, including any nested classes.

From the Java Language Specification section 6.6.1 :

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM