摘要:写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); } }

查询资料,深入学习

参考网络资料深入学习

静态内部类和非静态内部类之间到底有什么不同呢?下面是两者间主要的不同。

  • (1)内部静态类不需要有指向外部类的引用。但非静态内部类需要持有对外部类的引用。
  • (2)非静态内部类能够访问外部类的静态和非静态成员。静态类不能访问外部类的非静态成员。他只能访问外部类的静态成员。
  • (3)一个非静态内部类不能脱离外部类实体被创建,一个非静态内部类可以访问外部类的数据和方法,因为他就在外部类里面

点击关注,第一时间了解华为云新鲜技术~

Java变异出现错误:No enclosing instance of type XXX is accessible的更多相关文章

  1. Java编译时出现No enclosing instance of type XXX is accessible.

    今天在编译Java程序的时候出现以下错误: No enclosing instance of type Main is accessible. Must qualify the allocation ...

  2. 【转】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 ...

  3. 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   ...

  4. 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,在编译写书上一个例子时,由于书上的代码只有一部分,于是就自己补了一 ...

  5. 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 ...

  6. 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 ...

  7. 【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. ...

  8. 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 ...

  9. 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 ...

  10. No enclosing instance of type Hello is accessible

    1.static 关键字 修饰的成员被所有对象共享(包括成员变量和方法). 修饰的成员优先于对象存在. 存储于方法区(共享数据区)的静态区中. 静态方法只能访问静态成员. 静态方法中不可以使用this ...

随机推荐

  1. .NET周刊【10月第2期 2023-10-08】

    国内文章 起风了,NCC 云原生项目孵化计划 https://www.cnblogs.com/liuhaoyang/p/ncc-the-wind-rises.html 2016年,我和几位朋友发起了. ...

  2. Redis 6 学习笔记 2 —— 简单了解订阅和发布(Pub/Sub),JDK17环境下用Jedis 4.3.1连接Redis并模拟验证码发送

    REDIS pubsub -- Redis中国用户组(CRUG) 什么是发布和订阅 Redis发布订阅是一种通信模式:发送者(Pub)发送消息,订阅者(Sub)接收消息.Redis客户端可以订阅任意数 ...

  3. vscode/sublime 语法高亮定义和代码段的区别

    vscode插件数据格式基于json,sublime插件数据格式基于xml.sublime插件的官方文档说的不清楚,相关教程也很难找,遇到的一些坑记录一下 语法定义文件对比 同样使用TextMate定 ...

  4. ACAM 学习笔记 | 附 YbtOJ 全部题解

    怎么有人现在才学 ACAM 呢. 好像比 SAM 简单挺多啊,也不记得当时是哪里看不懂. AC 自动机() 自动 AC 机(✘) 概述 ACAM(Aho–Corasick Automaton),是用来 ...

  5. shell脚本之语句(条件、循环)

    条件语句 1.测试 使用[]时要使用空格,注意格式  格式1:test 条件表达式  格式2:[ 条件表达式 ]#注意空格  注意[]空格,否则会失败  测试 是否成功使用 $?返回值来判断  [ 操 ...

  6. 阿里发布AI编码助手:通义灵码,兼容 VS Code、IDEA等主流编程工具

    今天是阿里云栖大会的第一天,相信场外的瓜,大家都吃过了.这里就不说了,有兴趣可以看看这里:云栖大会变成相亲现场,最新招婿鄙视链来了... . 这里主要说说阿里还发布了一款AI编码助手,对于我们开发者来 ...

  7. 单元测试之Mockito+Junit使用和总结

    https://www.letianbiji.com/java-mockito/mockito-thenreturn.html Mockito 使用 thenReturn 设置方法的返回值 thenR ...

  8. 用python计算圆周率PI,并显示进度条

    用python计算圆周率PI ‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‮‬‪‬‪‬‪ ...

  9. 用友NC产品接口开发,通过轻易云数据集成平台快速调用

    通过用友NC产品的 UAP V63平台.插件相关处理.相关业务逻辑处理课程目标与要求课程内容课程目标与要求业务逻辑处理外部系统信息设置节点新建外部系统默认匹配规则:仅按对照表:外部系统数据与UAP.接 ...

  10. [USACO2007FEBS] Cow Party S

    题目描述 寒假到了,\(n\) 头牛都要去参加一场在编号为 \(x\) 的牛的农场举行的派对,农场之间有 \(m\) 条有向路,每条路都有一定的长度. 每头牛参加完派对后都必须回家,无论是去参加派对还 ...