java java内部类创建对象的问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5272957/
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
problem creating object of inner class in java
提问by bunkdeath
Here is the code.
这是代码。
public class Test {
class InnerClass{
}
public static void main(String[] args){
InnerClass ic = new InnerClass();
}
}
It says the error message
它说错误信息
non-static variable this cannot be referenced from a static context
after creation of object ic.
Can anyone give me the reason?
谁能给我理由?
Thanks
谢谢
回答by skaffman
InnerClass
needs to be static
itself, i.e.
InnerClass
需要是static
它自己,即
public class Test {
static class InnerClass{
}
public static void main(String[] args){
InnerClass ic = new InnerClass();
}
}
If InnerClass
is notstatic
, it can only be instantiated in the context of a parent instance of Test
. The rather baroque syntax for this is:
如果InnerClass
是没有static
,它只能在父实例的上下文实例Test
。相当巴洛克式的语法是:
public class Test {
class InnerClass{
}
public static void main(String[] args){
Test test = new Test();
InnerClass ic = test.new InnerClass();
}
}
回答by krtek
Your inner class depends on an instance of the Test
class. main is a static method, thus you can't create an instance of InnerClass
.
你的内部类依赖于类的一个实例Test
。main 是一个静态方法,因此您不能创建InnerClass
.
I think you want to declare your inner class as static
:
我认为您想将内部类声明为static
:
class Test {
static class InnerClass { }
public static void main(String[] args){
InnerClass ic = new InnerClass();
}
}
More information about nested classes : http://download.oracle.com/javase/tutorial/java/javaOO/nested.html
有关嵌套类的更多信息:http: //download.oracle.com/javase/tutorial/java/javaOO/nested.html
Short explanation
简短说明
There's mainly two types of nested classes : "static nested class" and "inner class"
主要有两种类型的嵌套类:“静态嵌套类”和“内部类”
Static nested class are used to logically group two classes and can be used to increase encapsulation. They can be used like any other classes and, except for visibility, they don't have any particular access to the outer class fields. They can logically be instantiated in a static context.
静态嵌套类用于在逻辑上对两个类进行分组,并可用于增加封装性。它们可以像任何其他类一样使用,并且除了可见性之外,它们对外部类字段没有任何特定的访问权限。它们可以逻辑地在静态上下文中实例化。
Inner class (ie not static) are bound to a particular instanceof the outer class. This means you must havean instance of the outer class to instantiate the inner class. Have a look at Skaffman second code chunk for an instantiation example. Since inner classes are bound to an instance of the outer class, they have access to every field relative to this particular instance.
内部类(即非静态)绑定到外部类的特定实例。这意味着你必须有一个外部类的实例来实例化内部类。查看 Skaffman 第二个代码块以获取实例化示例。由于内部类绑定到外部类的实例,因此它们可以访问与此特定实例相关的每个字段。
I hope all this is now clearer.
我希望这一切现在更清楚了。
回答by sgokhales
First, a "nested" class is static, and an "inner" class is not.
首先,“嵌套”类是静态的,而“内部”类则不是。
A static class is shared between all instances of the enclosing class (thus static fields are shared between all instances), while each instance has its own version of a non-static inner class.
Non-static inner classes are stored with the enclosing object (rather than class) and can only be accessed via an instance.
静态类在封闭类的所有实例之间共享(因此静态字段在所有实例之间共享),而每个实例都有自己的非静态内部类版本。
非静态内部类与封闭对象(而不是类)一起存储,并且只能通过实例访问。