这牌你文章主要给大家分享解决Java变异出现错误,具体的饥饿绝方案请看下面文章的内容,需要的朋友可以参考一下,希望能帮助到你
这牌你文章主要给大家分享解决Java变异出现错误,具体的饥饿绝方案请看下面文章的内容,需要的朋友可以参考一下,希望能帮助到你
这牌你文章主要给大家分享解决Java变异出现错误,具体的饥饿绝方案请看下面文章的内容,需要的朋友可以参考一下,希望能帮助到你
先记录下问题现象,写java代码时遇到下面的编译错误。
|
1
2
3
|
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). |
代码如下:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
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初始化个节点。
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
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。
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
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); }} |
如下所示:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
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); }} |
到此这篇关于解决Java变异出现错误No enclosing instance of type XXX is accessible的文章就介绍到这了,更多相关解决Java错误No enclosing instance of type XXX is accessible内容请搜索米米素材网以前的文章或继续浏览下面的相关文章希望大家以后多多支持米米素材网!
原文链接:https://juejin.cn/post/7012086906921943048
发表评论