出现:No enclosing instance of type Test_Static is accessible. Must qualify the allocation with an enclosing instance of type Test_Static (e.g. x.new A() where x is an instance of Test_Static).
上面的编译错误:可能由于静态public static main调用类的非静态方法AA
有两种解决办法:
第一种:
将内部类AA定义成静态static的类。
第二种:
将内部类AA在Main类外边定义。
1、
public class Test_Static { static class AA{ public void print(){ System.out.println("调用类方法"); } } public static void main(String[] args) { AA aa=new AA(); aa.print(); } }
2、
class AAA{ public void print(){ System.out.println("调用类方法"); } } public class Test_Static { public static void main(String[] args) { AAA aa=new AAA(); aa.print(); } }