我查看了LinkedList的Java代码,注意到它使用了一个静态嵌套类Entry。

public class LinkedList<E> ... {
...

 private static class Entry<E> { ... }

}

为什么要使用静态嵌套类,而不是普通的内部类?

我能想到的唯一原因是,Entry不能访问实例变量,所以从OOP的角度来看,它有更好的封装。

但我想可能还有其他原因,可能是表现。可能是什么?

请注意。我希望我的术语是正确的,我将它称为静态内部类,但我认为这是错误的:http://java.sun.com/docs/books/tutorial/java/javaOO/nested.html


当前回答

静态与普通的原因之一与类加载有关。不能在其父类的构造函数中实例化内部类。

PS:我一直认为“嵌套”和“内部”是可以互换的。术语之间可能有细微的差别,但大多数Java开发人员都能理解。

其他回答

JVM knows no nested classes. Nesting is just syntactic sugar. Below images shows Java file: Below images show class files representation of the java file : Notice that 2 class files are generated, one for parent and another for nested class. Non-static nested class' objects have access to the enclosing scope. That access to the enclosing scope is maintained by holding an implicit reference of the enclosing scope object in the nested object Nested class is a way to represent the intent that the nested class type represents a component of the parent class. public class Message { private MessageType messageType; // component of parent class public enum MessageType { SENT, RECEIVE; } } class Otherclass { public boolean isSent(Message message) { if (message.getMessageType() == MessageType.SENT) { // accessible at other places as well return true; } return false; } } private static nested class represents Point#3 & the fact the nested type can only be the subcomponent to the parent class. It can't be used separately. public class Message { private Content content; // Component of message class private static class Content { // can only be a component of message class private String body; private int sentBy; public String getBody() { return body; } public int getSentBy() { return sentBy; } } } class Message2 { private Message.Content content; // Not possible } More details here.

我不知道性能上的差异,但正如你所说,静态嵌套类不是外围类实例的一部分。创建一个静态嵌套类似乎更简单,除非你真的需要它是一个内部类。

这有点像为什么在Java中我总是让我的变量是final的——如果它们不是final的,我知道它们有一些有趣的事情发生了。如果使用内部类而不是静态嵌套类,应该有一个很好的理由。

在构建器模式中使用静态内部类。静态内部类可以实例化只有私有构造函数的外部类。你不能对内部类做同样的事情,因为你需要在访问内部类之前创建外部类的对象。

class OuterClass {
    private OuterClass(int x) {
        System.out.println("x: " + x);
    }
    
    static class InnerClass {
        public static void test() {
            OuterClass outer = new OuterClass(1);
        }
    }
}

public class Test {
    public static void main(String[] args) {
        OuterClass.InnerClass.test();
        // OuterClass outer = new OuterClass(1); // It is not possible to create outer instance from outside.
    }
}

这将输出x: 1

你链接到的Sun页面在这两者之间有一些关键的区别:

A nested class is a member of its enclosing class. Non-static nested classes (inner classes) have access to other members of the enclosing class, even if they are declared private. Static nested classes do not have access to other members of the enclosing class. ... Note: 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.

不需要LinkedList。Entry是顶级类,因为它只被LinkedList使用(还有一些其他接口也有名为Entry的静态嵌套类,例如Map。入口-相同的概念)。因为它不需要访问LinkedList的成员,所以它是静态的是有意义的——这是一种更简洁的方法。

正如Jon Skeet所指出的,我认为如果你正在使用一个嵌套类,最好从它是静态的开始,然后根据你的使用情况决定它是否真的需要是非静态的。

非静态内部类会导致内存泄漏,而静态内部类会防止内存泄漏。如果外部类包含大量数据,则会降低应用程序的性能。