您的位置:首页 > 其它

No enclosing instance of type 类名 is accessible. Must qualify the allocation with an enclosing instan

2016-08-14 22:21 676 查看
下面是我写的代码,一个经典的内部类

public class Value{
public static void main(String[] args) {
Test y=new Test();
}
class Test{
public Test() {
System.out.println("yes");
}
}
}结果在编译阶段就报错了:
No enclosing instance of type
Value(类名) is accessible. Must qualify the allocation with an enclosing instance of type Value (e.g. x.new A() where x is an instance of Value).

没有可访问的内部类Value的实例,必须分配一个合适的内部类E的实例(如x.new A(),x必须是Value的实例。)

原来是因为我写的Value类是动态的,而public
static void main方法是静态的,我们没办法在静态方法内去实例化一个动态对象

因此最简单的方法就是把class Test 换成 static class Test

或者在别的动态方法内调用动态方法
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐