Java变异出现错误:No enclosing instance of type XXX is accessible
摘要:写java代码时遇到下面的编译错误。
本文分享自华为云社区《Java中出现No enclosing instance of type XXX is accessible问题》,作者:zhushy 。
错误代码和错误现象
先记录下问题现象,写java代码时遇到下面的编译错误。
No enclosing instance of type FileTree is accessible. Must qualify the
allocation with an enclosing instance of type FileTree (e.g. x.new A()
where x is an instance of FileTree).
代码如下:
import java.util.Arrays;
import java.util.LinkedHashMap; public class FileTree {
class Node {
String name; public Node(String name) {
super();
this.name = name;
} LinkedHashMap<String, Node> map = new LinkedHashMap<String, Node>();
} public static void outputThreeFormat(String[] in) {
Arrays.sort(in);
Node root = new Node("/"); } public static void main(String[] args) {
String[] in = { "usr/local/lib64", "GAMES",
"usr/DRIVERS", "home", "var/log/" };
outputThreeFormat(in); } }
错误截图如下:

如何解决这些错误
错误的含义是,没有可以访问的外部实例enclosing instance。必须分配一个合适的外部类FileTree的实例(如x.new A(),x必须是FileTree的实例。)
结合出错的代码,很容易知道根源是什么:
- class Node是非静态内部类
- 而public static void outputThreeFormat(String[] in)是静态方法
- 静态方法是不能直接访问非静态类的。
可以不使用内部类
可以把class Node作为外部类定义,这样在FileTree类中不管是静态还是非静态方法都可以直接new Node初始化个节点。
import java.util.Arrays;
import java.util.LinkedHashMap; class Node {
String name; public Node(String name) {
super();
this.name = name;
} LinkedHashMap<String, Node> map = new LinkedHashMap<String, Node>();
} public class FileTree { public static void outputThreeFormat(String[] in) {
Arrays.sort(in);
Node root = new Node("/"); } public static void main(String[] args) {
String[] in = { "usr/local/lib64", "GAMES", "usr/DRIVERS", "home", "var/log/" };
outputThreeFormat(in); } }
可以使用静态内部类
可以把class Node作为静态内部类定义,即static class Node。
import java.util.Arrays;
import java.util.LinkedHashMap; public class FileTree {
static class Node {
String name; public Node(String name) {
super();
this.name = name;
} LinkedHashMap<String, Node> map = new LinkedHashMap<String, Node>();
} public static void outputThreeFormat(String[] in) {
Arrays.sort(in);
Node root = new Node("/"); } public static void main(String[] args) {
String[] in = { "usr/local/lib64", "GAMES",
"usr/DRIVERS", "home", "var/log/" };
outputThreeFormat(in); } }
使用非静态内部类时,使用外部类的实例进行调用
如下所示。
import java.util.Arrays;
import java.util.LinkedHashMap; public class FileTree {
class Node {
String name; public Node(String name) {
super();
this.name = name;
} LinkedHashMap<String, Node> map = new LinkedHashMap<String, Node>();
} public static void outputThreeFormat(String[] in) {
Arrays.sort(in);
FileTree ft=new FileTree();
Node root = ft.new Node("/"); } public static void main(String[] args) {
String[] in = { "usr/local/lib64", "GAMES",
"usr/DRIVERS", "home", "var/log/" };
outputThreeFormat(in); } }
查询资料,深入学习
参考网络资料深入学习
- https://www.cnblogs.com/kungfupanda/p/7239414.html java中的Static class
静态内部类和非静态内部类之间到底有什么不同呢?下面是两者间主要的不同。
- (1)内部静态类不需要有指向外部类的引用。但非静态内部类需要持有对外部类的引用。
- (2)非静态内部类能够访问外部类的静态和非静态成员。静态类不能访问外部类的非静态成员。他只能访问外部类的静态成员。
- (3)一个非静态内部类不能脱离外部类实体被创建,一个非静态内部类可以访问外部类的数据和方法,因为他就在外部类里面
Java变异出现错误:No enclosing instance of type XXX is accessible的更多相关文章
- Java编译时出现No enclosing instance of type XXX is accessible.
今天在编译Java程序的时候出现以下错误: No enclosing instance of type Main is accessible. Must qualify the allocation ...
- 【转】Java出现No enclosing instance of type E is accessible. Must qualify the allocation with an enclosing
最近在看Java,在编译写书上一个例子时,由于书上的代码只有一部分,于是就自己补了一个内部类.结果编译时出现:No enclosing instance of type E is accessible ...
- Java出现No enclosing instance of type E is accessible. Must qualify the allocation with an enclosing
Java出现No enclosing instance of type E is accessible. Must qualify the allocation with an enclosing ...
- Java出现No enclosing instance of type E is accessible. Must qualify the allocation with an enclosing--转
原文:http://blog.csdn.net/sunny2038/article/details/6926079 最近在看Java,在编译写书上一个例子时,由于书上的代码只有一部分,于是就自己补了一 ...
- No enclosing instance of type Test is accessible. Must qualify the allocation with an enclosing instance of type Test (e.g. x.new A() where x is an instance of Test).
Java编写代码过程中遇到了一个问题,main方法中创建内部类的实例时,编译阶段出现错误,查看错误描述: No enclosing instance of type Test is accessibl ...
- No enclosing instance of type test8 is accessible. Must qualify the allocation with an enclosing instance of type test8 (e.g. x.new A() where x is an
在编译一个例子时,结果编译时出现: No enclosing instance of type test8 is accessible. Must qualify the allocation wit ...
- 【eclipse】No enclosing instance of type A is accessible. Must qualify the allocation with an enclosing instance of type A
用 eclipse 写 Java 代码时出现了这个问题,详细如下: No enclosing instance of type TestParsingLinkedList is accessible. ...
- No enclosing instance of type test is accessible. Must qualify the allocation with an enclosing inst
今日遇到一个报错如下: No enclosing instance of type test is accessible. Must qualify the allocation with an en ...
- No enclosing instance of type Outer is accessible. Must qualify the allocation with an enclosing instance of type Outer (e.g. x.new A() where x is an instance of Outer)
之前看内部类的时候没发现这个问题,今天写代码的时候遇到,写个最简单的例子: 下面这一段代码 红色的部分就是编译报错: No enclosing instance of type Outer is ac ...
- No enclosing instance of type Hello is accessible
1.static 关键字 修饰的成员被所有对象共享(包括成员变量和方法). 修饰的成员优先于对象存在. 存储于方法区(共享数据区)的静态区中. 静态方法只能访问静态成员. 静态方法中不可以使用this ...
随机推荐
- JavaScript:对象的三个属性
每一个对象都有与之相关的原型(prototype).类(class)和可扩展性(extension attribute). 原型 prototype 对象的原型属性是用来继承属性的.通过对象直接量创建 ...
- HTML5的重要内容-1
HTML学习笔记-1 (一):first-child和:first-of-type :first-child第一个元素 :first-of-type第一个某种类型元素 (二):only-child和: ...
- 自动化混沌工程 ChaosMeta V0.6 版本发布
混沌工程 ChaosMeta 的全新版本 V0.6.0 现已正式发布!该版本包含了许多新特性和增强功能,在编排界面提供了包括流量注入.度量等各类节点的支持,可视化支撑演练全流程.解决混沌工程原则中&q ...
- 浅谈仓储UI自动化之路
1 分层测试 分层测试:就是不同的时间段,不同的团队或团队使用不同的测试用例对产品不同的关注点进行测试.一个系统/产品我们最先看到的是UI层,也就是外观或者说整体,这些是最上层,最上层依赖下面的服务层 ...
- 本地MinIO存储服务Java远程调用上传文件
MinIO是一款高性能.分布式的对象存储系统,它可以100%的运行在标准硬件上,即X86等低成本机器也能够很好的运行MinIO.它的优点包括高性能.高可用性.易于部署和管理.支持多租户等. Cpola ...
- FP-Growth算法全解析:理论基础与实战指导
本篇博客全面探讨了FP-Growth算法,从基础原理到实际应用和代码实现.我们深入剖析了该算法的优缺点,并通过Python示例展示了如何进行频繁项集挖掘. 关注TechLead,分享AI全维度知识.作 ...
- Linux机器自建账号并赋予sudo权限,同时修改远程端口
默认使用root账号来操作Linux有一定风险,因此需要自建账号并赋予sudo权限,方便使用 登录为root用户后,创建账号 adduser <username> Ubuntu系统会同时要 ...
- 使用Netty实现文件传输的HTTP服务器和客户端
现在我们来用netty实现文件传输的HTTP服务器和客户端 pom依赖文件: <?xml version="1.0" encoding="UTF-8"?& ...
- [GDOI22pj1A] 邹忌讽秦王纳谏
时间空间限制: 1 秒, 256 MB 齐国人邹忌对齐国国君齐威王说,大王身边的人会因为私情.利益等原因而对大王阿谀奉承,所以不能光听好话,只有广泛接受群众的批评意见,才不会被蒙蔽双眼,齐国才能强盛. ...
- Android Studio 学习-第三章 Activity 第一组
事先申明:所有android 类型的学习记录全部基于<第一行代码 Android>第三版,在此感谢郭霖老师的书籍帮助. 1.手动创建Activity 在Project类型目录中寻找到 项目 ...