解决No enclosing instance of type * is accessible
写一个内部类,并在构造函数中初始化时,遇到报错,搜索问题后发现,有网友出现过类似的问题,下面这个是说的浅显明白的,并确实解决了问题。于是,以下内容照搬过来,不再多费键盘了。
public class Test_drive {
  public static void main(String[] args){
    A a = new A();              //报错
    B b = new B();              //报错
    System.out.println(b instanceof A);
  }
  class A{
    int a;
  }
  class B extends A{
  }
}
上面两个语句报错信息如下:
No enclosing instance of type Test_drive is accessible. Must qualify the allocation with an enclosing instance of type Test_drive (e.g. x.new A() where x is an instance of Test_drive).
在overflow上面查找到了类似的问题:http://stackoverflow.com/questions/9560600/java-no-enclosing-instance-of-type-foo-is-accessible/9560633#9560633
下面简单说一下我的理解:
在这里,A和B都是Test_drive的内部类,类似于普通的实例变量,如类的静态方法不可以直接调用类的实例变量。在这里,内部类不是静态的内部类,所以,直接赋值(即实例化内部类),所以程序报错。
解决的方法可以有以下两种:
(1)将内部类定义为static,即为静态类
(2)将A a = new A();B b = new B();改为:
Test_drive td = new Test_drive();
A a = td.new A();
B b = td.new B();
解决No enclosing instance of type * is accessible的更多相关文章
- 再学Java 之 解决No enclosing instance of type * is accessible
		
深夜,临睡前写了个小程序,出了点小问题 public class Test_drive { public static void main(String[] args){ A a = new A(); ...
 - No enclosing instance of type is accessible. Must qualify the allocation with an enclosing instance of type LeadRestControllerTest (e.g. x.new A() where x is an instance of ).
		
java - No enclosing instance is accessible. Must qualify the allocation with an enclosing instance o ...
 - java中"no enclosing instance of type * is accessible"的解决方法
		
这种情况一般发生在“在静态方法里面使用内部类” 测试代码: public class Test { public static void main(String[] args) { A a = new ...
 - 【转】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 ...
 - Java出现No enclosing instance of type E is accessible. Must qualify the allocation with an enclosing--转
		
原文:http://blog.csdn.net/sunny2038/article/details/6926079 最近在看Java,在编译写书上一个例子时,由于书上的代码只有一部分,于是就自己补了一 ...
 - 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 ...
 - Java编译时出现No enclosing instance of type XXX is accessible.
		
今天在编译Java程序的时候出现以下错误: No enclosing instance of type Main is accessible. Must qualify the allocation ...
 - 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 ...
 
随机推荐
- Android之View绘制流程源码分析
			
版权声明:本文出自汪磊的博客,转载请务必注明出处. 对于稍有自定义View经验的安卓开发者来说,onMeasure,onLayout,onDraw这三个方法都不会陌生,起码多少都有所接触吧. 在安卓中 ...
 - CSS滤镜效果
			
使用 filter: blur() 生成毛玻璃效果 使用 filter: drop-shadow() 生成整体阴影效果 使用 filter: opacity() 生成透明度 blur生成阴影 通常我们 ...
 - Java基础机试题
			
package day8;import java.util.Scanner;/** * Java基础机试题 * @author:lyrand * */public class convert { ...
 - JavaWeb之Maven配置
			
Maven和C#的nuget类似,可以通过设置就能引入框架等第三方,方便又省事.Java中使用Maven来管理第三方.今天尝试着配置了一下. 一.JDK的安装 关于JDK的安装可以查看百度经验,设置P ...
 - 【转】缓存淘汰算法系列之2——LFU类
			
原文地址 :http://www.360doc.com/content/13/0805/16/13247663_304916783.shtml 1. LFU类 1.1. LFU 1.1.1. 原理 L ...
 - java:凯撒密码及String的应用
			
一,凯撒密码 古罗马皇帝凯撒在打仗时曾使用过以下方法加密军事情报 现在用java实现 程序设计思想: 1,字符串首先要转化为字符数组,才能依次加密 2,当原来的字符为X,Y,Z时,加密后要转化为A,B ...
 - css 文字和子元素水平垂直居中
			
关于水平垂直居中,这是一个很简单的问题,但是很多时候,往往简单的东西,反而做不出来.这就是基础不扎实的缘故吧,我参照一些资料,总结了水平垂直居中的几种方法如下: 1 .文字水平垂直居中 这个比较简单, ...
 - PHP操作Memcached
			
一.PHP连接Memcached: 一个简单的使用示例: $memcache = new Memcache; $memcache->connect("127.0.0.1",1 ...
 - JavaScript+HTML5 实现打地鼠小游戏
			
一.游戏简介 打地鼠这个游戏相信大家都不陌生,也是童年时候一款经典的游戏.本次游戏的编写是以html文件形式完成的,并且使用HBulider软件进行编写,使用谷歌浏览器展示效果,游戏将会采用JavaS ...
 - sqoop1.4.6导出oracle实例
			
1.导入odbj6.jar到$SQOOP_HOME/lib目录下. 2.sqoop import --append --connect jdbc:oracle:thin:@219.216.110.12 ...