我查看了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


当前回答

静态嵌套类就像任何其他外部类一样,因为它不能访问外部类成员。

为了方便打包,我们可以将静态嵌套类合并到一个外部类中以提高可读性。除此之外,静态嵌套类没有其他用例。

这种用法的例子,你可以在Android R.java(资源)文件中找到。 android的Res文件夹包含布局(包含屏幕设计),可绘制文件夹(包含用于项目的图像),值文件夹(包含字符串常量)等。

因为所有的文件夹都是Res文件夹的一部分,android工具生成一个R.java(资源)文件,其中内部包含许多静态嵌套类的每个内部文件夹。

下面是在android中生成的R.java文件的外观和感觉: 这里他们只是为了包装方便而使用。

/* AUTO-GENERATED FILE.  DO NOT MODIFY.
 *
 * This class was automatically generated by the
 * aapt tool from the resource data it found.  It
 * should not be modified by hand.
 */

package com.techpalle.b17_testthird;

public final class R {
    public static final class drawable {
        public static final int ic_launcher=0x7f020000;
    }
    public static final class layout {
        public static final int activity_main=0x7f030000;
    }
    public static final class menu {
        public static final int main=0x7f070000;
    }
    public static final class string {
        public static final int action_settings=0x7f050001;
        public static final int app_name=0x7f050000;
        public static final int hello_world=0x7f050002;
    }
}

其他回答

内部类的优势——

一次性使用 支持和改进封装 readibility 私有字段访问

没有外部类的存在,内部类就不存在。

class car{
    class wheel{

    }
}

内部类有四种类型。

正常的内部类 局部内部类 匿名内部类 静态内部类

点——

from static inner class ,we can only access static member of outer class. Inside inner class we cananot declare static member . inorder to invoke normal inner class in static area of outer class. Outer 0=new Outer(); Outer.Inner i= O.new Inner(); inorder to invoke normal inner class in instance area of outer class. Inner i=new Inner(); inorder to invoke normal inner class in outside of outer class. Outer 0=new Outer(); Outer.Inner i= O.new Inner(); inside Inner class This pointer to inner class. this.member-current inner class outerclassname.this--outer class for inner class applicable modifier is -- public,default, final,abstract,strictfp,+private,protected,static outer$inner is the name of inner class name. inner class inside instance method then we can acess static and instance field of outer class.

10.内部类在静态方法中,那么我们只能访问的静态字段

外部类。

class outer{

    int x=10;
    static int y-20;

    public void m1() {
        int i=30;
        final j=40;

        class inner{

            public void m2() {
                // have accees x,y and j
            }
        }
    }
}

在我看来,当你看到一个内部类时,问题应该是反过来的——它真的需要是一个内部类吗?它有额外的复杂性和隐式(而不是显式和更清晰的)引用包含类的实例?

请注意,作为一个c#爱好者,我有偏见——c#没有等价的内部类,尽管它有嵌套类型。我还不能说我错过了内部类:)

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

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

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

这有点像为什么在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