Jok*_*ker 2 java inner-classes
根据 JLS:
内部类是未显式或隐式声明为静态的嵌套类。内部类不能声明静态初始值设定项或成员接口。
但是我下面的代码编译成功。
class A {
interface B {
class C { // Inner class having static variables.
static int d; // Static variable
static {
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
有人可以帮助我理解这种行为吗
找到相同的 JLS 规格:
8.5.2 - “成员接口总是隐式静态的”
9.5 - “接口可能包含成员类型声明(第 8.5 节)。接口中的成员类型声明是隐式静态和公共的”
这意味着上面的代码在道德上等同于(隐式修饰符用大写字母书写):
class A {
STATIC interface B {
PUBLIC STATIC class C { //It's a static class - that's why static members are legal (like a toplevel class but nested)
static int d; //Static variable
static {} //Static initializer
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
78 次 |
| 最近记录: |