解決Java變異出現錯誤No enclosing instance of type XXX is accessible

一、錯誤代碼和錯誤現象

先記錄下問題現象,寫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)是靜態方法
  • 靜態方法是不能直接訪問非靜態類的。

1、可以不使用內部類

可以把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);

 }

}

2、可以使用靜態內部類

可以把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);

 }

}

3、使用非靜態內部類時,使用外部類的實例進行調用

如下所示:

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內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!

推薦閱讀: