报错的完整信息:
No enclosing instance of type OutterClass is accessible. Must qualify the allocation with an enclosing instance of type OutterClass (e.g. x.new A() where x is an instance of OutterClass).
错误的出现背景:
Java
在类中添加内部类,并在main函数中通过语句:
OutterClass.InnerClass innerClass = new OutterClass.InnerClass();
试图构造内部类对象时出现。
完整结构如下:
public class OutterClass {
class InnerClass {
void print() {
System.out.println("this is innerClass");
}
}
public static void main(String[] args) {
OutterClass.InnerClass innerClass = new OutterClass.InnerClass();
innerClass.print();
}
}