java - 静态嵌套类可以访问外部类的私有(private)构造函数

标签 java

stated那:

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.



那么如何解释这个静态嵌套类可以访问其封闭类的私有(private)构造函数呢?

最佳答案

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



因为任何 private 的范围和可访问性成员(字段、构造函数或方法)是其声明类的完整主体,包括任何嵌套类。

来自 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.

关于java - 静态嵌套类可以访问外部类的私有(private)构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23111745/

相关文章:

java - 如何使用 Java 代码启动 Tomcat 服务器?

java - 将 thymeleaf 变量处理为 html 代码而不是文本

java - 如何为Kotlin扩展功能的接收者添加KDoc注释(Java中的第一个参数,Kotlin中的 `this`)

java - 是否可以使用 try-catch 语句修补内存泄漏?

Java while(input.hasnextline) 循环不退出?

java - Android 线程因未知原因停止

java - 如何导入我在 jython 中创建的 java 类并调用方法

java - 无法从 JSON 格式的 gremlin 客户端检索查询结果

JavaFX 8 - 警报未打开

java - 初始化 map 、列表等