解決Java變異出現(xiàn)錯(cuò)誤No enclosing instance of type XXX is accessible
一、錯(cuò)誤代碼和錯(cuò)誤現(xiàn)象
先記錄下問題現(xiàn)象,寫java代碼時(shí)遇到下面的編譯錯(cuò)誤。
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);
}
}
錯(cuò)誤截圖如下:

二、如何解決這些錯(cuò)誤
錯(cuò)誤的含義是,沒有可以訪問的外部實(shí)例enclosing instance。必須分配一個(gè)合適的外部類FileTree的實(shí)例(如x.new A(),x必須是FileTree的實(shí)例。)
結(jié)合出錯(cuò)的代碼,很容易知道根源是什么:
class Node是非靜態(tài)內(nèi)部類- 而
public static void outputThreeFormat(String[] in)是靜態(tài)方法 - 靜態(tài)方法是不能直接訪問非靜態(tài)類的。
1、可以不使用內(nèi)部類
可以把class Node作為外部類定義,這樣在FileTree類中不管是靜態(tài)還是非靜態(tài)方法都可以直接new Node初始化個(gè)節(jié)點(diǎn)。
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);
}
}
2、可以使用靜態(tài)內(nèi)部類
可以把class Node作為靜態(tài)內(nèi)部類定義,即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);
}
}
3、使用非靜態(tài)內(nèi)部類時(shí),使用外部類的實(shí)例進(jìn)行調(diào)用
如下所示:
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);
}
}
到此這篇關(guān)于解決Java變異出現(xiàn)錯(cuò)誤No enclosing instance of type XXX is accessible的文章就介紹到這了,更多相關(guān)解決Java錯(cuò)誤No enclosing instance of type XXX is accessible內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
使用Java第三方實(shí)現(xiàn)發(fā)送短信功能
這篇文章主要介紹了使用Java第三方實(shí)現(xiàn)發(fā)送短信功能,在一些開發(fā)中,經(jīng)常需要有給用戶發(fā)送短信接收驗(yàn)證碼的功能,那么在Java中該如何實(shí)現(xiàn)呢,今天我們就一起來看一看2023-03-03
SpringBoot開發(fā)技巧之使用AOP記錄日志示例解析
這篇文章主要為大家介紹了SpringBoot開發(fā)技巧之如何利用AOP記錄日志的示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2021-10-10
SpringBoot打印啟動(dòng)時(shí)異常堆棧信息詳解
在本篇文章里小編給大家整理的是關(guān)于SpringBoot打印啟動(dòng)時(shí)異常堆棧信息,有需要的朋友們可以學(xué)習(xí)下。2019-11-11
Netty分布式pipeline管道Handler的添加代碼跟蹤解析
這篇文章主要介紹了Netty分布式pipeline管道Handler的添加代碼跟蹤解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-03-03
SpringBoot通過@MatrixVariable進(jìn)行傳參詳解
這篇文章主要介紹了SpringBoot使用@MatrixVariable傳參,文章圍繞@MatrixVariable展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-06-06
Java?數(shù)據(jù)交換?Json?和?異步請(qǐng)求?Ajax詳解
Json(JavaScript Object Notation)是一種輕量級(jí)的數(shù)據(jù)交換格式,采用鍵值對(duì)的形式來表示數(shù)據(jù),它廣泛應(yīng)用于Web開發(fā)中,特別適合于前后端數(shù)據(jù)傳輸和存儲(chǔ),這篇文章主要介紹了Java數(shù)據(jù)交換Json和異步請(qǐng)求Ajax,需要的朋友可以參考下2023-09-09
java http token請(qǐng)求代碼實(shí)例
這篇文章主要介紹了java http token請(qǐng)求,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03

