用下面的一幅图来进一步理解它们之间的关系,如下:
注意:
(1)除了static member class可以使用static field与static method、static block外,其它都不可以用static修改。
(2)A member interface is implicitly static
(§9.1.1). It is permitted for the declaration of a member interface to redundantly specify the static
modifier.
(3)Nested enum types are implicitly static
. It is permissible to explicitly declare a nested enum type to be static
.This implies that it is impossible to define a local (§14.3) enum, or to define an enum in an inner class (§8.1.3).
(4)The access modifier public
(§6.6) pertains only to top level classes (§7.6) and to member classes (§8.5), not to local classes (§14.3) or anonymous classes (§15.9.5).
(5)The access modifiers protected
and private
(§6.6) pertain only to member classes within a directly enclosing class or enum declaration (§8.5).
(6)The modifier static
pertains only to member classes (§8.5.1), not to top level or local or anonymous classes.
(7)A local class is a nested class (§8) that is not a member of any class and that has a name (§6.2, §6.7).
public void test(){
class A{
class B{}
}
}
按照定义,则类B应该不为local class。等待确认.....
interface IOuter{
int a = 2; // only public, static & final are permitted
class INestedClass{ // only public ,static are permitted
}
}
public class TopLevelClass{
class NestedClass{ // only static,public,protected,private,final are permitted
}
class InnerClass{ // only public,protected,private,final are permitted
}
public void test(){
class LocalClass{ // only abstract or final is permitted
}
}
}
关于类继承内部类:
class WithInner {
class Inner { }
}
class InheritInner extends WithInner.Inner {
// 第一种写法
InheritInner(WithInner wi) {
wi.super();
}
// 第二种写法
InheritInner() {
new WithInner().super();
}
public static void main(String[] args) {
WithInner wi = new WithInner();
InheritInner ii = new InheritInner(wi);
}
}
这是在《JAVA编程思想》上看到的一道例题,是关于继承inner classes(内隐类)的。由于inner class的构造函数必须连接到一个reference指向outer class对象身上,所以当你继承inner class时,事情便稍微复杂些。问题出在“指向outer class对象”的那个神秘reference必须被初始化。但derived class之内不存在可连接的缺省对象,这个问题的答案是,使用专用语法,明确产生该关联性:
参考:
(1)深入理解为什么Java中方法内定义的内部类可以访问方法中的局部变量
(2)https://blog.youkuaiyun.com/he_world/article/details/50252039