解決Java變異出現(xiàn)錯誤No enclosing instance of type XXX is accessible
一、錯誤代碼和錯誤現(xiàn)象
先記錄下問題現(xiàn)象,寫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
是非靜態(tài)內部類- 而
public static void outputThreeFormat(String[] in)
是靜態(tài)方法 - 靜態(tài)方法是不能直接訪問非靜態(tài)類的。
1、可以不使用內部類
可以把class Node
作為外部類定義,這樣在FileTree類中不管是靜態(tài)還是非靜態(tài)方法都可以直接new Node初始化個節(jié)點。
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)內部類
可以把class Node
作為靜態(tà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)內部類時,使用外部類的實例進行調用
如下所示:
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變異出現(xiàn)錯誤No enclosing instance of type XXX is accessible
的文章就介紹到這了,更多相關解決Java錯誤No enclosing instance of type XXX is accessible
內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
springboot定時任務@Scheduled執(zhí)行多次的問題
這篇文章主要介紹了springboot定時任務@Scheduled執(zhí)行多次問題的解決,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-10-10集群環(huán)境中使用ehcache_動力節(jié)點Java學院整理
這篇文章主要為大家詳細介紹了集群環(huán)境中使用ehcache的相關資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-08-08詳解使用IntelliJ IDEA新建Java Web后端resfulAPI模板
這篇文章主要介紹了詳解使用IntelliJ IDEA新建Java Web后端resfulAPI模板,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-08-08Spring boot + LayIM + t-io 實現(xiàn)文件上傳、 監(jiān)聽用戶狀態(tài)的實例代碼
這篇文章主要介紹了Spring boot + LayIM + t-io 實現(xiàn)文件上傳、 監(jiān)聽用戶狀態(tài)的實例代碼,需要的朋友可以參考下2017-12-12springboot集成nacos報錯:get data from Nacos
這篇文章給大家介紹了springboot集成nacos報錯:get data from Nacos error,dataId:null.yaml的原因及解決方法,如果又遇到相同問題的朋友可以參考閱讀本文2023-10-10