栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

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();
	}
}
转载请注明:文章转载自 www.mshxw.com
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号