Abstract: encountered the following compilation error when writing java code.
This article is shared from the Huawei Cloud Community "No enclosing instance of type XXX is accessible in ", author: zhushy.
Error code and error phenomenon
Record the problem first. When writing java code, you encounter the following compilation error.
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).
code show as below:
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);
}
}
The error screenshot is as follows:
How to resolve these errors
The meaning of the error is that there is no external enclosing instance that can be accessed. An instance of a suitable external class FileTree must be allocated (such as x.new A(), x must be an instance of FileTree.)
Combined with the wrong code, it is easy to know what the root cause is:
- class Node is a non-static inner class
- And public static void outputThreeFormat(String[] in) is a static method
- Static methods cannot directly access non-static classes.
Can not use internal classes
You can define class Node as an external class, so that no matter whether it is a static or non-static method in the FileTree class, you can directly initialize a node with 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);
}
}
You can use static inner classes
Class Node can be defined as a static internal class, that is, 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);
}
}
When using a non-static inner class, use an instance of the outer class to call
As follows.
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);
}
}
Inquiry for information, in-depth study
In-depth study with reference to network materials
• https://www.cnblogs.com/kungfupanda/p/7239414.html Static class in java
What is the difference between static inner classes and non-static inner classes? The following are the main differences between the two.
• (1) Internal static classes do not need to have references to external classes. But the non-static inner class needs to hold a reference to the outer class.
• (2) Non-static inner classes can access the static and non-static members of the outer class. Static classes cannot access non-static members of external classes. He can only access the static members of the external class.
• (3) A non-static internal class cannot be created without the external class entity, a non-static internal class can access the data and methods of the external class because it is in the external class
Click to follow and learn about Huawei Cloud's fresh technology for the first time~
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。