小编典典
您可能是混淆static与abstract作为kihero说,或者你是得过且过与具有一个类的概念static方法(这只是碰巧有静态方法的类)。
静态嵌套类只是一个嵌套类,不需要其封闭类的实例。如果您熟悉C ,则C中的 所有
类都是“静态”类。在Java中,默认情况下,嵌套类不是静态的(此非静态种类也称为“内部类”),这意味着它们需要其外部类的实例,并在隐藏字段的后台进行跟踪,但是这使内部类可以引用其关联的封闭类的字段。
public class Outer {
public class Inner { }
public static class StaticNested { }
public void method () {
// non-static methods can instantiate static and non-static nested classes
Inner i = new Inner(); // 'this' is the implied enclosing instance
StaticNested s = new StaticNested();
}
public static void staticMethod () {
Inner i = new Inner(); //
StaticNested s = new StaticNested(); // ok: no enclosing instance needed
// but we can create an Inner if we have an Outer:
Outer o = new Outer();
Inner oi = o.new Inner(); // ok: 'o' is the enclosing instance
}
}
实际上,默认情况下,我实际上将所有嵌套类都声明为静态,除非我特别需要访问封闭类的字段。
2020-11-13