可以在 Java 中实例化静态嵌套类吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18293857/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can a static nested class be instantiated in Java?
提问by user1031431
From Oracle's Java tutorialsI've found this text:
从 Oracle 的 Java教程中,我找到了这段文字:
As with class methods and variables, a static nested class is associated with its outer class. And like static class methods, a static nested class cannot refer directly to instance variables or methods defined in its enclosing class — it can use them only through an object reference.
Note: 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.
Static nested classes are accessed using the enclosing class name:
OuterClass.StaticNestedClass
For example, to create an object for the static nested class, use this syntax:
OuterClass.StaticNestedClass nestedObject = new OuterClass.StaticNestedClass();
与类方法和变量一样,静态嵌套类与其外部类相关联。和静态类方法一样,静态嵌套类不能直接引用在其封闭类中定义的实例变量或方法——它只能通过对象引用来使用它们。
注意:静态嵌套类与其外部类(和其他类)的实例成员交互,就像任何其他顶级类一样。实际上,静态嵌套类在行为上是一个顶层类,为了方便打包,它嵌套在另一个顶层类中。
使用封闭类名访问静态嵌套类:
外部类.静态嵌套类
例如,要为静态嵌套类创建对象,请使用以下语法:
OuterClass.StaticNestedClass nestedObject = new OuterClass.StaticNestedClass();
I thought it is not possible to instantiate a static class, so I don't really understand the sentence in bold.
我认为实例化静态类是不可能的,所以我不太理解粗体的句子。
Do you have any idea what it means?
你知道这意味着什么吗?
采纳答案by Jason C
You are either confusing static
with abstract
as kihero says, or you are muddling the concept with a class that has static
methods (which is just a class that happens to have static methods).
您要么像 kihero 所说的那样混淆static
,要么混淆了abstract
具有static
方法的类(这只是一个碰巧具有静态方法的类)的概念。
A static nested class is just a nested class that doesn't require an instance of its enclosing class. If you are familiar with C++, allclasses in C++ are "static" classes. In Java, nested classes are not static by default (this non-static variety is also called an "inner class"), which means they require an instance of their outer class, which they track behind the scenes in a hidden field -- but this lets inner classes refer to fields of their associated enclosing class.
静态嵌套类只是一个嵌套类,不需要其封闭类的实例。如果您熟悉 C++,那么C++ 中的所有类都是“静态”类。在 Java 中,嵌套类默认不是静态的(这种非静态变体也称为“内部类”),这意味着它们需要外部类的实例,它们在隐藏字段中在幕后跟踪——但是这让内部类可以引用它们关联的封闭类的字段。
public class Outer {
public class Inner { }
public static class StaticNested { }
public void method () {
// non-static methods can instantiate static and non-static nested classes
Inner i = new Inner(); // 'this' is the implied enclosing instance
StaticNested s = new StaticNested();
}
public static void staticMethod () {
Inner i = new Inner(); // <-- ERROR! there's no enclosing instance, so cant do this
StaticNested s = new StaticNested(); // ok: no enclosing instance needed
// but we can create an Inner if we have an Outer:
Outer o = new Outer();
Inner oi = o.new Inner(); // ok: 'o' is the enclosing instance
}
}
Lots of other examples at How to instantiate non static inner class within a static method
如何在静态方法中实例化非静态内部类中的许多其他示例
I actually declare all nested classes static by default unless I specifically need access to the enclosing class's fields.
我实际上默认情况下将所有嵌套类声明为静态,除非我特别需要访问封闭类的字段。
回答by kiheru
You are confusing static
with abstract
. Abstract classes can not be instantiated. static
is not a valid qualifier for top level classes, but the meaning for inner classes is the one you quoted.
你static
对abstract
. 抽象类不能被实例化。static
不是顶级类的有效限定符,但内部类的含义是您引用的那个。
回答by Abhishek raut
Static nested classes are themselves not static at all. In java, no class is static. Static keyword in static nested classes implies that it is another static member of the outer class. But it is just another raw class . Thats why we can instantiate this class
静态嵌套类本身根本就不是静态的。在java中,没有类是静态的。静态嵌套类中的静态关键字暗示它是外部类的另一个静态成员。但它只是另一个原始类。这就是为什么我们可以实例化这个类
回答by Dai Niu
I guess you misunderstood the static class a little bit. It's true that every abstract class and interface cannot be instantiated, but you do can instantiate an static class. One thing you should notice is that every static class is a nested static class. You cannot just create a static class, as you can see: try to create a new class in eclipse
我猜你有点误解了静态类。确实,每个抽象类和接口都无法实例化,但您可以实例化静态类。您应该注意的一件事是每个静态类都是嵌套的静态类。您不能只创建一个静态类,如您所见: 尝试在 eclipse 中创建一个新类
A static class always belongs to the "parent class" which encloses it, and the difference between static and non-static class is: You can refer to the child static class just like a static property of the "parent class":
静态类始终属于包围它的“父类”,静态类和非静态类的区别在于:您可以像“父类”的静态属性一样引用子静态类:
ParentClass.NestedStaticClass nestedstatic = new ParentClass.NestedStaticClass();
but you can only make reference to the non-static nested class by instantiating a parent class, like this:
但是您只能通过实例化父类来引用非静态嵌套类,如下所示:
ParentClass parent = new ParentClass();
ParentClass.NestedClass nested = parent.new NestedClass();
The difference is just like that between the static and non-static field.
区别就像静态和非静态字段之间的区别。