Java:静态与内部类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1353309/
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
Java: Static vs inner class
提问by Abhishek Sanghvi
What is the difference between static and non-static nested class?
静态和非静态嵌套类有什么区别?
采纳答案by Brandon E Taylor
An inner class, by definition, cannot be static, so I am going to recast your question as "What is the difference between static and non-static nested classes?"
根据定义,内部类不能是静态的,因此我将您的问题重新定义为“静态和非静态嵌套类之间有什么区别?”
A non-static nested class has full access to the members of the class within which it is nested. A static nested class does not have a reference to a nesting instance, so a static nested class cannot invoke non-static methods or access non-static fields of an instance of the class within which it is nested.
非静态嵌套类对嵌套类的成员具有完全访问权限。静态嵌套类没有对嵌套实例的引用,因此静态嵌套类不能调用非静态方法或访问嵌套类的实例的非静态字段。
回答by Steve McLeod
Let's look in the source of wisdom for such questions: Joshua Bloch's Effective Java:
让我们看看这些问题的智慧来源:Joshua Bloch 的 Effective Java:
Technically, there is no such thing as a static inner class. According to Effective Java, the correct terminology is a static nested class. A non-static nested class is indeed an inner class, along with anonymous classes and local classes.
从技术上讲,没有静态内部类这样的东西。根据Effective Java,正确的术语是静态嵌套类。非静态嵌套类确实是内部类,还有匿名类和本地类。
And now to quote:
现在引用:
Each instance of a non-static nestedclass is implicitly associated with an enclosing instanceof its containing class... It is possible to invoke methods on the enclosing instance.
非静态嵌套类的每个实例都与其包含类的封闭实例隐式关联......可以在封闭实例上调用方法。
A static nested class does not have access to the enclosing instance. It uses less space too.
静态嵌套类无权访问封闭实例。它也占用更少的空间。
回答by DigitalRoss
Discussing nestedclasses...
讨论嵌套类...
The difference is that a nested class declaration that is also static can be instantiated outside of the enclosing class.
不同之处在于,同样是静态的嵌套类声明可以在封闭类之外实例化。
When you have a nested class declaration that is not static, Java won't let you instantiate it except via the enclosing class. The object created out of the inner class is linked to the object created from the outer class, so the inner class can reference the fields of the outer.
当您有一个不是 static的嵌套类声明时,Java 不会让您实例化它,除非通过封闭类。从内部类创建的对象链接到从外部类创建的对象,因此内部类可以引用外部类的字段。
But if it's static, then the link does not exist, the outer fields cannot be accessed (except via an ordinary reference like any other object) and you can therefore instantiate the nested class by itself.
但是如果它是static,则链接不存在,无法访问外部字段(除非像任何其他对象一样通过普通引用),因此您可以自己实例化嵌套类。
回答by Vijay Kumar
static inner class: can declare static & non static members but can only access static members of its parents class.
静态内部类:可以声明静态和非静态成员,但只能访问其父类的静态成员。
non static inner class: can declare only non static members but can access static and non static member of its parent class.
非静态内部类:只能声明非静态成员,但可以访问其父类的静态和非静态成员。
回答by Emil
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.
静态嵌套类与其外部类(和其他类)的实例成员交互,就像任何其他顶级类一样。实际上,静态嵌套类在行为上是一个顶层类,为了方便打包,它嵌套在另一个顶层类中。
回答by Emil
There are two differences between static inner and non static inner classes.
静态内部类和非静态内部类之间有两个区别。
In case of declaring member fields and methods, non static inner class cannot have static fields and methods. But, in case of static inner class, can have static and non static fields and method.
The instance of non static inner class is created with the reference of object of outer class, in which it has defined, this means it has enclosing instance. But the instance of static inner class is created without the reference of Outer class, which means it does not have enclosing instance.
在声明成员字段和方法的情况下,非静态内部类不能有静态字段和方法。但是,在静态内部类的情况下,可以有静态和非静态字段和方法。
非静态内部类的实例是通过外部类的对象的引用创建的,在该对象中定义了它,这意味着它具有封闭的实例。但是静态内部类的实例是在没有外部类的引用的情况下创建的,这意味着它没有封闭的实例。
See this example
看这个例子
class A
{
class B
{
// static int x; not allowed here
}
static class C
{
static int x; // allowed here
}
}
class Test
{
public static void main(String… str)
{
A a = new A();
// Non-Static Inner Class
// Requires enclosing instance
A.B obj1 = a.new B();
// Static Inner Class
// No need for reference of object to the outer class
A.C obj2 = new A.C();
}
}
回答by kathir
An inner class cannot be static, so I am going to recast your question as "What is the difference between static and non-static nested classes?".
内部类不能是静态的,因此我将您的问题重新定义为“静态和非静态嵌套类之间有什么区别?”。
as u said here inner class cannot be static... i found the below code which is being given static....reason? or which is correct....
正如你在这里所说的,内部类不能是静态的......我发现下面的代码是静态的......原因?或者哪个是正确的....
Yes, there is nothing in the semantics of a static nested type that would stop you from doing that. This snippet runs fine.
是的,静态嵌套类型的语义中没有任何内容会阻止您这样做。这个片段运行良好。
public class MultipleInner {
static class Inner {
}
public static void main(String[] args) {
for (int i = 0; i < 100; i++) {
new Inner();
}
}
}
this is a code posted in this website...
这是在这个网站上发布的代码...
for the question---> Can a Static Nested Class be Instantiated Multiple Times?
对于问题---> 静态嵌套类可以多次实例化吗?
answer was--->
答案是--->
Now, of course the nested type can do its own instance control (e.g. private constructors, singleton pattern, etc) but that has nothing to do with the fact that it's a nested type. Also, if the nested type is a static enum, of course you can't instantiate it at all.
现在,当然嵌套类型可以执行自己的实例控制(例如私有构造函数、单例模式等),但这与它是嵌套类型的事实无关。此外,如果嵌套类型是静态枚举,当然您根本无法实例化它。
But in general, yes, a static nested type can be instantiated multiple times.
但总的来说,是的,静态嵌套类型可以多次实例化。
Note that technically, a static nested type is not an "inner" type.
请注意,从技术上讲,静态嵌套类型不是“内部”类型。
回答by abishkar bhattarai
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.
静态内部类不能访问封闭类的非静态成员。它可以直接访问封闭类的静态成员(实例字段和方法),就像在不创建对象的情况下获取值的过程风格一样。
静态内部类可以声明静态成员和非静态成员。静态方法可以访问主类的静态成员。但是,它不能访问非静态内部类成员。要访问非静态内部类的成员,必须创建非静态内部类的对象。
非静态内部类不能声明静态字段和静态方法。它必须以静态或顶级类型声明。这样做时会出现此错误,说“静态字段只能在静态或顶级类型中声明”。
非静态内部类可以以取值的过程方式访问封闭类的静态成员和非静态成员,但不能访问静态内部类的成员。
封闭类在创建内部类对象之前不能访问内部类的成员。如果主类访问非静态类的成员,它可以创建非静态内部类的对象。
如果主类访问静态内部类的成员,它有两种情况:
- 情况一:对于静态成员,可以使用静态内部类的类名
- 情况二:对于非静态成员,可以创建静态内部类的实例。