静态嵌套类和非静态嵌套类的区别是什么?


当前回答

Static inner class cannot access non-static members of enclosing class. It can directly access static members (instance field and methods) of enclosing class same like the procedural style of getting value without creating object. Static inner class can declare both static and non-static members. The static methods have access to static members of main class. However, it cannot access non-static inner class members. To access members of non-static inner class, it has to create object of non-static inner class. Non-static inner class cannot declare static field and static methods. It has to be declared in either static or top level types. You will get this error on doing so saying "static fields only be declared in static or top level types". Non-static inner class can access both static and non-static members of enclosing class in procedural style of getting value, but it cannot access members of static inner class. The enclosing class cannot access members of inner classes until it creates an object of inner classes. IF main class in accessing members of non-static class it can create object of non-static inner class. If main class in accessing members of static inner class it has two cases: Case 1: For static members, it can use class name of static inner class Case 2: For non-static members, it can create instance of static inner class.

其他回答

讨论嵌套类……

不同之处在于,同样是静态的嵌套类声明可以在外围类之外实例化。

当你有一个非静态的嵌套类声明时,Java不允许你实例化它,除非通过外围类。从内部类创建的对象被链接到从外部类创建的对象,因此内部类可以引用外部类的字段。

但如果它是静态的,则链接不存在,外部字段不能被访问(除非通过像任何其他对象一样的普通引用),因此您可以通过自身实例化嵌套类。

让我们看看Joshua Bloch的《Effective Java》:

从技术上讲,没有静态内部类这样的东西。根据Effective Java,正确的术语是静态嵌套类。非静态嵌套类实际上是一个内部类,与匿名类和局部类一样。

现在引用一下:

非静态嵌套类的每个实例都隐式关联 使用其包含类的封闭实例…这是可能的 调用封闭实例上的方法。

静态嵌套类不能访问外围实例。它也占用更少的空间。

静态内部类:可以声明静态和非静态成员,但只能访问父类的静态成员。

非静态内部类:只能声明非静态成员,但可以访问父类的静态和非静态成员。

Static inner class cannot access non-static members of enclosing class. It can directly access static members (instance field and methods) of enclosing class same like the procedural style of getting value without creating object. Static inner class can declare both static and non-static members. The static methods have access to static members of main class. However, it cannot access non-static inner class members. To access members of non-static inner class, it has to create object of non-static inner class. Non-static inner class cannot declare static field and static methods. It has to be declared in either static or top level types. You will get this error on doing so saying "static fields only be declared in static or top level types". Non-static inner class can access both static and non-static members of enclosing class in procedural style of getting value, but it cannot access members of static inner class. The enclosing class cannot access members of inner classes until it creates an object of inner classes. IF main class in accessing members of non-static class it can create object of non-static inner class. If main class in accessing members of static inner class it has two cases: Case 1: For static members, it can use class name of static inner class Case 2: For non-static members, it can create instance of static inner class.

静态嵌套类与其外部类(和其他类)的实例成员交互,就像任何其他顶级类一样。实际上,静态嵌套类在行为上是一个顶级类,为了打包方便而嵌套在另一个顶级类中。