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 ...
随机推荐
- Python JSON 使用指南:解析和转换数据
JSON 是一种用于存储和交换数据的语法.JSON 是文本,使用 JavaScript 对象表示法编写. Python 中的 JSON Python 有一个内置的 json 包,可用于处理 JSON ...
- dotnet 探究 SemanticKernel 的 planner 的原理
在使用 SemanticKernel 时,我着迷于 SemanticKernel 强大的 plan 能力,通过 plan 功能可以让 AI 自动调度拼装多个模块实现复杂的功能.我特别好奇 Semant ...
- Java系列:Java8 新特性:强大的 Stream API(创建 Stream、中间操作、终止操作)
Java8中有两大最为重要的改变.第一个是 Lambda 表达式:另外一个则是 Stream API. Stream API ( java.util.stream) 把真正的函数式编程风格引入到Jav ...
- [NOIP 考前备战] 线段树刷题
备战线段树 T1 AcWing .1275. 最大数 查询最大值 + 单点修改 #include <bits/stdc++.h> #define int long long using n ...
- Postgresql——jsonb类型
Postgresql Json 最近有个功能,需要用到 NoSQL 数据库.但是又不想因为这个小小的功能给系统增加一个 MongoDB 数据库,于是就想到了 Postgresql 支持 JSON 类型 ...
- 【Javaweb】动态web工程目录介绍
src 存放自己编写的Java源代码 web 专门用来存放web工程的资源文件(html页面.css文件.js文件等等) WEB-INF 是一个受服务器保护的目录,浏览器无法直接访问此目录的内容 we ...
- 多维度分析数据的软件,BI软件不就是吗
BI软件(Business Intelligence Software)是一种用于多维度分析数据的工具,可以帮助企业从海量数据中提取有价值的洞察力,并为决策者和业务用户提供可视化.交互式的报表和仪表盘 ...
- 吉特日化MES & Redis 运行远程访问的配置
在吉特日化MES系统部署实施过程中,经常需要配置Redis需要运行远程IP访问Redis.使用Redis的目的主要是为了解决缓存的问题,同时解决打印过程中推送数据的问题. 一. Redis 的安装目录 ...
- 一文读懂遗传算法(附python)
几天前,我着手解决一个实际问题--大型超市销售问题.在使用了几个简单模型做了一些特征工程之后,我在排行榜上名列第 219 名. 虽然结果不错,但是我还是想做得更好.于是,我开始研究可以提高分数的优化方 ...
- 使用汇编和反汇编引擎写一个x86任意地址hook
最简单的Hook 刚开始学的时候,用的hook都是最基础的5字节hook,也不会使用hook框架,hook流程如下: 构建一个jmp指令跳转到你的函数(函数需定义为裸函数) 保存被hook地址的至少5 ...