Java 报错 No enclosing instance of type B is accessible. Must qualify the allocation with an enclosin
前言
在写 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();
}
}
點(diǎn)擊查看更多內(nèi)容
為 TA 點(diǎn)贊
評論
評論
共同學(xué)習(xí),寫下你的評論
評論加載中...
作者其他優(yōu)質(zhì)文章
正在加載中
感謝您的支持,我會繼續(xù)努力的~
掃碼打賞,你說多少就多少
贊賞金額會直接到老師賬戶
支付方式
打開微信掃一掃,即可進(jìn)行掃碼打賞哦