java 为什么允许静态嵌套类对象的实例化?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12836506/
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
Why instantiation of static nested class object is allowed?
提问by Parag Meshram
I have started learning Java language for Android Application developement.
我已经开始学习用于 Android 应用程序开发的 Java 语言。
As per my understanding based on static class, we cannot instantiate object of static class.
根据我基于静态类的理解,我们无法实例化静态类的对象。
But why instantiation of static nested class object is allowed in following situaltion?
但是为什么在以下情况下允许静态嵌套类对象的实例化?
class EnclosingClass
{
//...
class static StaticInnerClass
{
//...
}
}
Why we can create object of inner class if it is marked as static?
如果标记为静态,为什么我们可以创建内部类的对象?
EnclosingClass.StaticInnerClass s = new EnclosingClass.StaticInnerClass()
回答by Jon Skeet
As per my understanding based on static class, we cannot instantiate object of static class.
根据我基于静态类的理解,我们无法实例化静态类的对象。
Your understanding of the meaning of "static class" is incorrect. Basically a "static class" in Javais a nested class which doesn't have an implicit reference to an instance of the containing class. See section 8.5.1 of the JLSfor more information, in particular:
您对“静态类”含义的理解是错误的。Java 中的“静态类”基本上是一个嵌套类,它没有对包含类的实例的隐式引用。有关更多信息,请参阅JLS 的第 8.5.1 节,特别是:
The static keyword may modify the declaration of a member type C within the body of a non-inner class or interface T. Its effect is to declare that C is not an inner class. Just as a static method of T has no current instance of T in its body, C also has no current instance of T, nor does it have any lexically enclosing instances.
static 关键字可以在非内部类或接口 T 的主体内修改成员类型 C 的声明。它的作用是声明 C 不是内部类。正如 T 的静态方法在其主体中没有 T 的当前实例一样,C 也没有 T 的当前实例,也没有任何词法封闭实例。
Perhaps you were thinking of static classes in C#, which are completely different?
也许您正在考虑 C# 中的静态类,它们完全不同?
回答by Alessandro
Why we can create object of inner class if it is marked as static?
如果标记为静态,为什么我们可以创建内部类的对象?
You may need to use a nested class
in a static
context, for example:
您可能需要nested class
在static
上下文中使用 a ,例如:
public class Test {
public static void main(String args[]) {
InnerClass innerClass = new InnerClass();
}
class InnerClass {
}
}
In this case, when you try to instantiate the innerClass
you get the error:
在这种情况下,当您尝试实例化时,innerClass
您会收到错误消息:
No enclosing instance of type Test is accessible. Must qualify the allocation with an enclosing instance of type Test (e.g. x.new A() where x is an instance of Test).
没有可访问类型 Test 的封闭实例。必须使用 Test 类型的封闭实例来限定分配(例如 xnew A(),其中 x 是 Test 的一个实例)。
To avoid this, you could instantiate an object of type Test
and create an instance of innerClass
from it:
为了避免这种情况,您可以实例化一个类型的对象Test
并innerClass
从中创建一个实例:
Test test = new Test();
InnerClass innerClass = test.new InnerClass();
or better, declare also the innerClass
as static and instantiate it in a static context:
或者更好的是,将 也声明innerClass
为静态并在静态上下文中实例化它:
public class Test {
public static void main(String args[]) {
InnerClass innerClass = new InnerClass();
}
static class InnerClass {
}
}
回答by Alex Muni
check it, maybe it can help you Nested Classes
检查它,也许它可以帮助您 嵌套类