报错 No enclosing instance of type B is accessible. Must qualify the allocation with an enclosin"/>
Java 报错 No enclosing instance of type B is accessible. Must qualify the allocation with an enclosin
Title: Java 报错 No enclosing instance of type B is accessible. Must qualify the allocation with an enclosing
前言
在写 Java 并发的 Demo ,写的时候发现了一个报错: Java 出现 No enclosing instance of type TestB is accessible. Must qualify the allocation with an enclosing instance of type TestB(e.g. x.new A() where is an instance of TestB)
主要是我基础不好,这里通过网上查询发现了问题所在,这里就记录一下,先看代码:
public class TestB {public class ThreadA {}public static void main(String[] args) {ThreadA teata = new ThreadA();}
}
这里我偷懒,声明了内部类,然后在主程序 public static main 中调用,类的静态方法不能直接调用动态方法。
解决办法一
内部类改为静态的
public class TestB {public static class ThreadA { }public static void main(String[] args) {ThreadA teata = new ThreadA();}
}
解决办法二
不用内部类
public class TestB{public static void main(String[] args) {TestB teata = new TestB();}
}
更多推荐
Java 报错 No enclosing instance of type B is accessible. Must qualify the allocati
发布评论