为了账号安全,请及时绑定邮箱和手机立即绑定

报错介绍 No enclosing instance of type B is accessible.

标签:
Java

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();
	}
}
点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
JAVA开发工程师
手记
粉丝
30
获赞与收藏
154

关注作者,订阅最新文章

阅读免费教程

  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消