Java 报错 No enclosing instance of type B is accessible. Must qualify the allocation with an enclosin

编程入门 行业动态 更新时间:2024-10-14 00:26:39

Java <a href=https://www.elefans.com/category/jswz/34/1771188.html style=报错 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

本文发布于:2024-02-07 06:23:47,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1753966.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:报错   instance   enclosing   Java   type

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!