enclosin专题

No enclosing instance of type ObjectTest is accessible. Must qualify the allocation with an enclosin

刚刚写代码运行出错, 报的问题: No enclosing instance of type ObjectTest is accessible. Must qualify the allocation with an enclosing instance of type ObjectTest (e.g. x.new A() where x is an instance of ObjectTest

Java出现No enclosing instance of type Main is accessible. Must qualify the allocation with an enclosin

经过查证,是因为内部类是动态的(无static关键字修饰),而main方法是静态的,普通的内部类对象隐含地保存了一个引用,指向创建它的外围类对象,所以要在static方法(类加载时已经初始化)调用内部类的必须先创建外部类。即应该这样创建“DanymicTest test = new StaticCallDynamic().new DanymicTest();”其中StaticCallDynami