This question already has an answer here:
这个问题已经有了答案:
- Java inner class and static nested class 23 answers
- Java内部类和静态嵌套类23答案
What is the difference between static and non static inner class?
静态内部类和非静态内部类的区别是什么?
8 个解决方案
#1
490
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.
非静态嵌套类可以完全访问其嵌套的类的成员。静态嵌套类没有对嵌套实例的引用,因此静态嵌套类不能调用非静态方法或访问嵌套类的类实例的非静态字段。
#2
110
Let's look in the source of wisdom for such questions: Joshua Bloch's Effective Java:
让我们来看看这些问题的智慧源泉:Joshua Bloch的有效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.
从技术上讲,不存在静态内部类。根据有效Java,正确的术语是静态嵌套类。非静态嵌套类实际上是一个内部类,以及匿名类和本地类。
And now to quote:
现在报价:
Each instance of a nonstatic [nested] class is implicitly associated with an enclosing instance of 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.
静态嵌套类不能访问封闭实例。它也使用更少的空间。
#3
52
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();
}
}
#4
11
-
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
- 案例1:对于静态成员,它可以使用静态内部类的类名
- Case 2: For non-static members, it can create instance of static inner class.
- 案例2:对于非静态成员,它可以创建静态内部类的实例。
#5
10
Discussing nested classes...
讨论嵌套类…
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.
当您有一个非静态的嵌套类声明时,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.
但是如果它是静态的,那么这个链接就不存在,外部的字段不能被访问(除非通过像其他对象一样的普通引用),因此您可以自己实例化嵌套的类。
#6
4
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.
注意,从技术上讲,静态嵌套类型不是“内部”类型。
#7
3
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.
非静态内部类:只能声明非静态成员,但可以访问其父类的静态和非静态成员。
#8
3
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.
静态嵌套类与它的外部类(和其他类)的实例成员交互,就像任何其他*类一样。实际上,静态嵌套类在行为上是一个*类,为了便于打包,它被嵌套在另一个*类中。
#1
490
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.
非静态嵌套类可以完全访问其嵌套的类的成员。静态嵌套类没有对嵌套实例的引用,因此静态嵌套类不能调用非静态方法或访问嵌套类的类实例的非静态字段。
#2
110
Let's look in the source of wisdom for such questions: Joshua Bloch's Effective Java:
让我们来看看这些问题的智慧源泉:Joshua Bloch的有效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.
从技术上讲,不存在静态内部类。根据有效Java,正确的术语是静态嵌套类。非静态嵌套类实际上是一个内部类,以及匿名类和本地类。
And now to quote:
现在报价:
Each instance of a nonstatic [nested] class is implicitly associated with an enclosing instance of 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.
静态嵌套类不能访问封闭实例。它也使用更少的空间。
#3
52
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();
}
}
#4
11
-
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
- 案例1:对于静态成员,它可以使用静态内部类的类名
- Case 2: For non-static members, it can create instance of static inner class.
- 案例2:对于非静态成员,它可以创建静态内部类的实例。
#5
10
Discussing nested classes...
讨论嵌套类…
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.
当您有一个非静态的嵌套类声明时,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.
但是如果它是静态的,那么这个链接就不存在,外部的字段不能被访问(除非通过像其他对象一样的普通引用),因此您可以自己实例化嵌套的类。
#6
4
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.
注意,从技术上讲,静态嵌套类型不是“内部”类型。
#7
3
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.
非静态内部类:只能声明非静态成员,但可以访问其父类的静态和非静态成员。
#8
3
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.
静态嵌套类与它的外部类(和其他类)的实例成员交互,就像任何其他*类一样。实际上,静态嵌套类在行为上是一个*类,为了便于打包,它被嵌套在另一个*类中。