Java变异出现错误:No enclosing instance of type XXX is accessible

Posted 华为云

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java变异出现错误:No enclosing instance of type XXX is accessible相关的知识,希望对你有一定的参考价值。

摘要:写java代码时遇到下面的编译错误。

本文分享自华为云社区《Java中出现No enclosing instance of type XXX is accessible问题》,作者:zhushy 。

错误代码和错误现象

先记录下问题现象,写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)是静态方法
  • 静态方法是不能直接访问非静态类的。

可以不使用内部类

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

	}

}

可以使用静态内部类

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

	}

}

使用非静态内部类时,使用外部类的实例进行调用

如下所示。

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);

	}

}

查询资料,深入学习

参考网络资料深入学习

静态内部类和非静态内部类之间到底有什么不同呢?下面是两者间主要的不同。

  • (1)内部静态类不需要有指向外部类的引用。但非静态内部类需要持有对外部类的引用。
  • (2)非静态内部类能够访问外部类的静态和非静态成员。静态类不能访问外部类的非静态成员。他只能访问外部类的静态成员。
  • (3)一个非静态内部类不能脱离外部类实体被创建,一个非静态内部类可以访问外部类的数据和方法,因为他就在外部类里面

点击关注,第一时间了解华为云新鲜技术~

以上是关于Java变异出现错误:No enclosing instance of type XXX is accessible的主要内容,如果未能解决你的问题,请参考以下文章

java出现No enclosing instance of type main is accessible. Must qualify the allocation with an enclosin

No Enclosing instance ...的原因和解决方法

No enclosing instance of type E is accessible. Must qualify the allocation with an enclosing

No enclosing instance of type test is accessible. Must qualify the allocation with an enclosing inst

Java中报错No enclosing instance of type caiquan is accessible. Must qualify the allocation with an encl

[Java A] – is not an enclosing class