是否可以在不与封闭 class 的实例关联的情况下使用非静态成员类型?

Can a nonstatic member type be used without association with an instance of the enclosing class?

是否可以在不与封闭 class 的实例关联的情况下使用非静态成员类型?

例如

class Outer {    
    class Inner {       

    }    
}

public class Demo {
    public static void main(String args[]) {

        Outer o = new Outer();
        Outer.Inner inner = o.new Inner();    

    }    
}

Outer.Inner inner = o.new Inner() 中,Outer.Inner 是否使用 Inner 而未关联到封闭 class 的实例?

是否有其他示例可以在不与封闭 class 的实例关联的情况下使用非静态成员类型?

谢谢

does Outer.Inner use Inner without association to an instance of the enclosing class?

在某种意义上,是的,因为Outer.Inner指的是类型。而且你永远不需要实例来使用 types.

我认为您错误地认为 Outer.Inner 不应该工作,因为有无限多的不同类型称为 Inner,由 Outer 的不同实例创建。你可能会想,如果我有两个不同的 Outero1o2 实例,它们将创建不同的 Inner 类型 .

但这完全不是真的。你完全可以这样做:

Outer o1 = new Outer();
Outer o2 = new Outer();
Outer.Inner inner1 = o1.new Inner();
Outer.Inner inner2 = o2.new Inner();
inner2 = inner1;

所以Outer.Inner只是一个类型。碰巧这种类型的实例需要 Outer 的实例才能创建。