No enclosing instance of type Demo is accessible. Must qualify the allocation with an enclosing instance of type Demo (e.g. x.new A() where x is an instance of Demo).

No enclosing instance of type Demo is accessible. Must qualify the allocation with an enclosing instance of type Demo (e.g. x.new A() where 
x is an instance of Demo).

在练习一Demo时,遇到上面的编译时错误

原因:main是Test类的static方法,按照常理它只能访问Test类中static资源,而class A是非static所以报错了。

解决方法一:给class F添加static属性。

解决方法二: 没有必要把F放在Demo中做内部类,将它移到Demo外部地方定义。

原文地址:https://www.cnblogs.com/zjfjava/p/6741868.html