您的位置:首页 > 编程语言 > Java开发

Java错误No enclosing instance of type E is accessible. Must qualify the allocation with an enclosing

2016-05-15 16:53 696 查看
最近在编写例子的时候,自己添加了一个内部类。结果编译时遇到一个小问题:No enclosing instance of type E is accessible. Must qualify the allocation with an enclosing instance of type E(e.g. x.new A() where x is an instance
of E).


错误提示:没有可访问的内部类E的实例,必须分配一个合适的内部类E的实例。非常郁闷,已经用new实例化了这个类.......。

经过仔细思考发现,我写的内部类是动态的(以public class开头)。而主程序是public static class main。在Java中,类中的静态方法不能直接调用动态方法。只有将某个内部类修饰为静态类,然后才能够在静态类中调用该类的成员变量与成员方法。所以为了方便起见,最简单的方法:将内部类由 public class
该为 public static class。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: