之前看内部类的时候没发现这个问题,今天写代码的时候遇到,写个最简单的例子:

下面这一段代码

红色的部分就是编译报错:

No enclosing instance of type Outer is accessible. Must qualify the allocation with an enclosing instance of type Outer (e.g. x.new A() where x is an instance of Outer).

根据提示,没有可以访问的实例Outer,必须分配一个合适的外部类实例以访问内部类。

正确的方式可以是:

public class Outer {
private int i = 10;
class Inner{
public void seeOuter(){
System.out.println(i);
}
}
public static void main(String[] args) {
Outer out = new Outer();
Inner in = out.new Inner();
in.seeOuter();
}
}

或者:

public class Outer {
private int i = 10;
static class Inner{
public void seeOuter(){
// 静态内部类不能直接访问外部类的非静态成员,但可以通过 new 外部类().成员 的方式访问
//System.out.println(i);这句话编译不过
System.out.println(new Outer().i);
}
} public static void main(String[] args) {
Inner in = new Inner();
in.seeOuter();
}
}

关于内部类

  • 依然是一个独立的类,在编译之后会内部类会被编译成独立的.class文件,但是前面冠以外部类的类命和$符号。
  • 内部类不能用普通的方式访问。内部类是外部类的一个成员,因此内部类可以自由地访问外部类的成员变量,无论是否是private的。
  • 成员内部类内不允许有任何静态声明!
  • 能够访问成员内部类的唯一途径就是通过外部类的对象!

有关内部类具体可参考

http://blog.csdn.net/ft305977550/article/details/8350708

仅仅记录编程中遇到的问题。

No enclosing instance of type Outer is accessible. Must qualify the allocation with an enclosing instance of type Outer (e.g. x.new A() where x is an instance of Outer)的更多相关文章

  1. 【转】Java出现No enclosing instance of type E is accessible. Must qualify the allocation with an enclosing

    最近在看Java,在编译写书上一个例子时,由于书上的代码只有一部分,于是就自己补了一个内部类.结果编译时出现:No enclosing instance of type E is accessible ...

  2. Java出现No enclosing instance of type E is accessible. Must qualify the allocation with an enclosing

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

  3. No enclosing instance of type test8 is accessible. Must qualify the allocation with an enclosing instance of type test8 (e.g. x.new A() where x is an

    在编译一个例子时,结果编译时出现: No enclosing instance of type test8 is accessible. Must qualify the allocation wit ...

  4. No enclosing instance of type Test is accessible. Must qualify the allocation with an enclosing instance of type Test (e.g. x.new A() where x is an instance of Test).

    Java编写代码过程中遇到了一个问题,main方法中创建内部类的实例时,编译阶段出现错误,查看错误描述: No enclosing instance of type Test is accessibl ...

  5. No enclosing instance of type Demo is accessible. Must qualify the allocation with an enclosing instance of type Demo (e.g. x.new A() where x is an instance of Demo).

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

  6. 【eclipse】No enclosing instance of type A is accessible. Must qualify the allocation with an enclosing instance of type A

    用 eclipse 写 Java 代码时出现了这个问题,详细如下: No enclosing instance of type TestParsingLinkedList is accessible. ...

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

    今日遇到一个报错如下: No enclosing instance of type test is accessible. Must qualify the allocation with an en ...

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

    在Java中,类中的静态方法不能直接调用动态方法.只有将某个内部类修饰为静态类,然后才能够在静态类中调用该类的成员变量与成员方法.所以在不做其他变动的情况下,最简单的解决办法是将public clas ...

  9. Java中报错No enclosing instance of type caiquan is accessible. Must qualify the allocation with an enclosing instance of type caiquan (e.g. x.new A() where x is an instance of caiquan).

    package test;import java.util.Scanner;import java.util.Random;public class caiquan { public static v ...

随机推荐

  1. Poj2946-The Warehouse(bfs+哈希)

    题目我就不粘贴了... 题意:给出地图,最大8*8,出口用'E'表示,空地用'.'表示,数字表示此处有多少个箱子,主人公的起点应该是在有箱子的地方,他可以朝四个方向移动,但是只有两种方式 一种是他移动 ...

  2. 利用内存结构及多线程优化多图片下载(IOS篇)

    利用内存结构及多线程优化多图片下载(IOS篇) 前言 下载地址, 后续发布, 请继续关注本blog 在IOS中,我们常常遇到多图片下载的问题.最简单的解决方案是直接利用别人写好的框架.但是这如同练武, ...

  3. 使用linux系统做路由转发

    使用linux系统(PC机)做路由转发 关键字:linux,Fedora,route,iptables,ip_forward 最近做网络实验,在实验过程中需要用到linux的转发功能,但是遇到一些问题 ...

  4. poj 2697 A Board Game(bfs+hash)

    Description Dao was a simple two-player board game designed by Jeff Pickering and Ben van Buskirk at ...

  5. Ant-进阶

    Ant的概念 可能有些读者并不连接什么是Ant以及入可使用它,但只要使用通过Linux系统得读者,应该知道make这个命令.当编译Linux内核及一些软件的源程序时,经常要用这个命令.Make命令其实 ...

  6. sql注入数据库修复方法

    1.第一种情况是 需要将指定的 注入字符串全部替换掉(仅替换注入的字符串为空) declare @delStr nvarchar(500) set @delStr='<script src=ht ...

  7. 谈谈UIView的几个layout方法-layoutSubviews、layoutIfNeeded、setNeedsLayout...

    最近在学习swift做动画,用到constraint的动画,用到layoutIfNeeded就去研究了下UIView的这几个布局的方法. 下面是做得一个动画,下载地址:https://github.c ...

  8. JAVA反射机制示例,读取excel数据映射到JAVA对象中

    import java.beans.PropertyDescriptor; import java.io.File; import java.io.FileInputStream; import ja ...

  9. ADO.NET程序访问数据的组件

    组成--数据集(内存中的数据库) --DataSet数据集 --DataTable数据表 --DataColumn数据列 --DataRow数据行 --DataView数据视图--NET数据提供程序 ...

  10. PHP学习笔记二十三【This】

    <?php Class Person { function test1() { $this->test2();//类里面的方法互相调用要加$this } protected functio ...