再学Java 之 解决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).
(1)在stackoverflow上面查找到了类似的问题:http://stackoverflow.com/questions/9560600/java-no-enclosing-instance-of-type-foo-is-accessible/9560633#9560633
(2)下面简单说一下我的理解:
在这里,A和B都是Test_drive的内部类,类似于普通的实例变量,如果类的静态方法不可以直接调用类的实例变量。在这里,内部类不是静态的内部类,所以,直接赋值(即实例化内部类),所以程序报错。
(3)解决的方法可以有以下两种:
(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();
附注:写到这里好困。如果大家有更好的理解,请在下面留言。谢谢。
再学Java 之 解决No enclosing instance of type * is accessible的更多相关文章
- 解决No enclosing instance of type * is accessible
写一个内部类,并在构造函数中初始化时,遇到报错,搜索问题后发现,有网友出现过类似的问题,下面这个是说的浅显明白的,并确实解决了问题.于是,以下内容照搬过来,不再多费键盘了. public class ...
- 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 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 ...
- 再学Java 之 interface的成员变量
前言:最近在学多线程,写“哲学家就餐问题(Dining Philosophers)”的时候,需要定义一个全局的变量,即哲学家的人数.常用的做法是在其中一个类中定义一个static final的变量,然 ...
- Java中的static(1)【持续更新】——关于Eclipse的No enclosing instance of type ... 错误的理解和改正
No enclosing instance of type SomeClass 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 ...
- 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,在编译写书上一个例子时,由于书上的代码只有一部分,于是就自己补了一 ...
随机推荐
- 优秀前端工程师必备: 非常常用的checkbox的骚操作---全选和单选demo
提要: 前端开发的时候, 经常会遇到表格勾选, 单个勾选判断是否全选的事情.趁着有时间, 总结一下以备不时之需! 就像下面这个栗子: 1 源代码: h5 // 全选框 <input type=& ...
- Redis总结和提取常用的和重要的命令
一:Redis的结构和其数据类型(注redis默认端口号是6379) 1)Redis可以部署多套(多个进程不同端口或直接部署在不同主机),每个Redis都可以有多个db,通过select来选择,默认的 ...
- Java 原始类型JComboBox的成员JComboBox(E())的调用 未经过检查
问题描述: 根据书上的代码 ,编译时候出现以下问题 自定义了一个字符数组: private String[] grades = {"1","2","3 ...
- spring 课程
官网 参考文档 // 1. Spring_HelloWorld 20:22 // 2. Spring_IOC&DI概述 08:07 // 3. Spring_配置 Bean 21:58 // ...
- 二)spring 集成 ehcache jgroups 集群
依赖 <dependency> <groupId>org.springframework</groupId> <artifactId>spring-co ...
- 基于MOD13A1的锡林郭勒草原近13年植被覆盖变化 分析
内蒙古师范大学地理科学学院 内蒙古师范大学遥感与地理信息系统重点实验室 摘要:本研究以内蒙古锡林郭勒草原为研究区,基于MOD13A1遥感数据,经过遥感预处理,得到研究区2001-2013年共13年夏季 ...
- 【转】启动、停止Windows服务的DOS命令
需要用管理员身份运行 在图形界面中启动.停止服务很方便,但是操作系统有时候会出故障,此时不妨使用原始的DOS命令启动.停止服务,也许会收到意想不到的效果的! 方法/步骤 1 开始→所有程序. 2 附件 ...
- HDU1501 Zipper(DFS) 2016-07-24 15:04 65人阅读 评论(0) 收藏
Zipper Problem Description Given three strings, you are to determine whether the third string can be ...
- codis服务部署前的操作及初始化
1.检查服务器ipv6模块是否打开,如果打开需要禁用ipv6,防止codis-dashbord连接zookeeper失败. 因为不确定codis-dashbord服务连接zookeeper使用ipv4 ...
- ActiveMQ5.0实战三:使用Spring发送,消费topic和queue消息
实战一 , 实战二 介绍了ActiveMQ的基本概念和配置方式. 本篇将通过一个实例介绍使用spring发送,消费topic, queue类型消息的方法. 不懂topic和queue的google 之 ...