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)
之前看内部类的时候没发现这个问题,今天写代码的时候遇到,写个最简单的例子:
下面这一段代码
红色的部分就是编译报错:
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)的更多相关文章
- 【转】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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 【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. ...
- 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 ...
- No enclosing instance of type E is accessible. Must qualify the allocation with an enclosing
在Java中,类中的静态方法不能直接调用动态方法.只有将某个内部类修饰为静态类,然后才能够在静态类中调用该类的成员变量与成员方法.所以在不做其他变动的情况下,最简单的解决办法是将public clas ...
- 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 ...
随机推荐
- 【hihocoder 1039 字符串消除】模拟
题目链接:http://hihocoder.com/problemset/problem/1039 题意:给定一个只由{A, B, C}组成的字符串s,长度为n, 故包含n+1个空隙:现要求在某个空隙 ...
- deciaml(十进制浮点运算)
# -*- coding: utf-8 -*- # ==================== #File: python #Author: python #Date: 2014 #========== ...
- jquery.validate详解一
jQuery校验 官网地址:http://bassistance.de/jquery-plugins/jquery-plugin-validation 一导入js库 <script src=&q ...
- redis的简单使用
一.简单使用Jedis 需要Jedis就从Maven获取吧! Maven Pom.xml <dependency> <groupId>redis.clients</gro ...
- Guice 学习(六)使用Provider注入服务( Provider Inject Service)
1.定义接口 package com.guice.providerInject; import com.google.inject.ProvidedBy; public interface Servi ...
- [置顶] Android项目组织和代码重用
在Android应用开发过程中,只要涉及两个或以上人的开发,就需要考虑分工和代码的组织和重用问题. 代码重用有三种方式: 1.APK: 2.JAR:通过Libs/ 和Build path集成,缺点是不 ...
- DOS 选择跳转实现、dos + bcp 双击导入和导出数据
DOS 选择跳转实现.dos + bcp 双击导入和导出数据 option.bat @echo off :Start2 cls goto Start :Start title Frequently U ...
- hdu 2825 Wireless Password(ac自己主动机&dp)
Wireless Password Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- [Python学习笔记][Python内置函数]
Python 常用内建函数 比较基础的列表 abs(x) 求绝对值 pow(x,y) 返回x的y次方,等同于x**y round(x[,小数位数]) 对x进行四舍五入,若不指定位数,则返回整数 chr ...
- Android编译过程详解(二)
通过上篇文章,我们分析了编译android时source build/envsetup.sh和lunch命令,在执行完上述两个命令后, 我们就可以进行编译android了. 1. make 执行ma ...