在定义一个内部类,编译时出现:
No enclosing instance of type A is accessible. Must qualify the allocation with an enclosing instance of type A(e.g. x.new A() where x is an instance of A)
出错的地方是我在实例化一个内部类对象的时候,比如 A a = new A();
A是自己定义的内部类
原因是主程序是public static void main(String[] args),是静态方法
而我在定义内部类的时候写的直接是 class A{...},相当于是动态的。静态方法main不能直接调用动态类中的动态方法。(实例化相当于是自动调用了类中的构造函数,是动态的方法。)
解决的办法是:将内部类声明为静态的,改写成 static class A{...}即可解决问题。