ITPub博客

首页 > IT职业 > IT职场 > 报错介绍 No enclosing instance of type

报错介绍 No enclosing instance of type

IT职场 作者:yellowduck007 时间:2021-09-09 10:38:01 0 删除 编辑

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();
	}
}

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/2611/viewspace-2824398/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2019-08-16

  • 博文量
    88
  • 访问量
    984