使用 Java 将内部类设为静态有什么好处?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16147405/
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
What's the advantage of making an inner class as static with Java?
提问by prosseek
I have an inner class in my Java class.
我的 Java 类中有一个内部类。
When I run find bugs, it recommends(warns) to make it as static.
当我运行find bugs 时,它建议(警告)将其设为静态。
What's the point of this warning? What's the advantage of making a inner class as static?
这个警告有什么意义?将内部类设为静态有什么好处?
采纳答案by Jeff Storey
If the nested class does not access any of the variables of the enclosing class, it can be made static. The advantage of this is that you do not need an enclosing instance of the outer class to use the nested class.
如果嵌套类不访问封闭类的任何变量,则可以将其设为静态。这样做的好处是您不需要外部类的封闭实例来使用嵌套类。
回答by Joeri Hendrickx
An inner class, by default, has an implicit reference to an object of the outer class. If you instantiate an object of this from the code of the outer class, this is all done for you. If you do otherwise you need to provide the object yourself.
默认情况下,内部类具有对外部类对象的隐式引用。如果您从外部类的代码中实例化 this 的对象,这一切都为您完成。如果你不这样做,你需要自己提供对象。
A static inner class does not have this.
静态内部类没有这个。
That means it can be instantiated outside the scope of an outer class object. It also means that if you 'export' an instance of the inner class, it will not prevent the current object to be collected.
这意味着它可以在外部类对象的范围之外实例化。这也意味着如果您“导出”内部类的实例,则不会阻止收集当前对象。
As a basic rule, if the inner class has no reason to access the outer one, you should make it static by default.
作为一项基本规则,如果内部类没有理由访问外部类,则应默认将其设为静态。
回答by TimK
A static inner class is a semantically simpler thing. It's just like a top-level class except you have more options for visibility (e.g. you can make it private).
静态内部类在语义上更简单。它就像一个顶级类,除了您有更多的可见性选项(例如,您可以将其设为私有)。
An important reason to avoid non-static inner classes is that they are more complex. There is the hidden reference to the outer class (maybe even more than one). And a simple name in a method of the inner class may now be one of three things: a local, a field, or a field of an outer class.
避免使用非静态内部类的一个重要原因是它们更复杂。存在对外部类的隐藏引用(甚至可能不止一个)。内部类的方法中的简单名称现在可能是三件事之一:本地、字段或外部类的字段。
An artifact of that complexity is that the hidden reference to the outer class can lead to memory leaks. Say the inner class is a listener and could be a static inner class. As long as the listener is registered, it holds a reference to the instance of the outer class, which may in turn hold on to large amounts of memory. Making the listener static may allow the outer instance to be garbage collected.
这种复杂性的一个工件是对外部类的隐藏引用可能导致内存泄漏。假设内部类是一个侦听器并且可以是一个静态内部类。只要侦听器被注册,它就会保存对外部类实例的引用,这反过来可能会占用大量内存。使侦听器静态可能允许对外部实例进行垃圾收集。
回答by kan
A Non-static inner class has an implicit reference to outer class. If you make the class as static, you could save some memory and code.
非静态内部类具有对外部类的隐式引用。如果将类设为静态,则可以节省一些内存和代码。
回答by Ramesh
We already have good answers, here are my 5 cents:
我们已经有了很好的答案,这是我的 5 美分:
Both static and non-static inner classes are used when we need to separate logical functionalities yet using the methods and variables of the outer class. Both of the inner classes have access to the private variables of the outer class.
当我们需要分离逻辑功能但使用外部类的方法和变量时,会使用静态和非静态内部类。两个内部类都可以访问外部类的私有变量。
Advantages of static inner class: 1) static classes can access the static variables from outer class 2) static classes can be treated like an independent class
静态内部类的优点: 1)静态类可以访问外部类的静态变量 2)静态类可以作为一个独立的类对待
Non-static inner class: 1) cannot use static members of the outer class 2) cannot be treated like an independent class
非静态内部类:1) 不能使用外部类的静态成员 2) 不能当作独立类对待
public class NestedClassDemo {
private int a = 100;
int b = 200;
private static int c = 500;
public NestedClassDemo() {
TestInnerStatic teststat = new TestInnerStatic();
System.out.println("const of NestedClassDemo, a is:"+a+", b is:"+b+".."+teststat.teststat_a);
}
public String getTask1(){
return new TestInnerClass().getTask1();
}
public String getTask2(){
return getTask1();
}
class TestInnerClass{
int test_a = 10;
TestInnerClass() {
System.out.println("const of testinner private member of outerlcass"+a+"..."+c);
}
String getTask1(){
return "task1 from inner:"+test_a+","+a;
}
}
static class TestInnerStatic{
int teststat_a = 20;
public TestInnerStatic() {
System.out.println("const of testinnerstat:"+teststat_a+" member of outer:"+c);
}
String getTask1stat(){
return "task1 from inner stat:"+teststat_a+","+c;
}
}
public static void main(String[] args){
TestInnerStatic teststat = new TestInnerStatic();
System.out.println(teststat.teststat_a);
NestedClassDemo nestdemo = new NestedClassDemo();
System.out.println(nestdemo.getTask1()+"...."+nestdemo.getTask2());
}
}
Accessing the static inner and non-static inner class from outside:
从外部访问静态内部和非静态内部类:
public class TestClass {
public static void main(String[] args){
NestedClassDemo.TestInnerClass a = new NestedClassDemo().new TestInnerClass();
NestedClassDemo.TestInnerStatic b = new NestedClassDemo.TestInnerStatic();
}
}
The official java doc for static inner class can be found at https://docs.oracle.com/javase/tutorial/java/javaOO/nested.html
静态内部类的官方 java 文档可以在https://docs.oracle.com/javase/tutorial/java/javaOO/nested.html找到
回答by chao sibada
Benefits of static inner classes:
静态内部类的好处:
- Instantiation of static inner class does not rely on external class guidance, and the memory overhead of instantiation.
- Static inner class does not hold external class guidance, does not affect the collection of external class, to avoid the extension of the external class in memory survival time leading to memory leakage.
- 静态内部类的实例化不依赖外部类的引导,实例化的内存开销较大。
- 静态内部类不持有外部类引导,不影响外部类的集合,避免外部类在内存存活时间的延长导致内存泄漏。