深夜,临睡前写了个小程序,出了点小问题

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的更多相关文章

  1. 解决No enclosing instance of type * is accessible

    写一个内部类,并在构造函数中初始化时,遇到报错,搜索问题后发现,有网友出现过类似的问题,下面这个是说的浅显明白的,并确实解决了问题.于是,以下内容照搬过来,不再多费键盘了. public class ...

  2. 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 ...

  3. java中"no enclosing instance of type * is accessible"的解决方法

    这种情况一般发生在“在静态方法里面使用内部类” 测试代码: public class Test { public static void main(String[] args) { A a = new ...

  4. 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 ...

  5. 再学Java 之 interface的成员变量

    前言:最近在学多线程,写“哲学家就餐问题(Dining Philosophers)”的时候,需要定义一个全局的变量,即哲学家的人数.常用的做法是在其中一个类中定义一个static final的变量,然 ...

  6. Java中的static(1)【持续更新】——关于Eclipse的No enclosing instance of type ... 错误的理解和改正

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

  7. 【转】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 ...

  8. 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   ...

  9. 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,在编译写书上一个例子时,由于书上的代码只有一部分,于是就自己补了一 ...

随机推荐

  1. Cache Algorithms

    1. 平均内存引用时间 T = average memory reference time m = miss ratio = 1 - (hit ratio) Tm = time to make a m ...

  2. (并查集 建立关系)食物链 -- POJ-- 1182

    链接: http://poj.org/problem?id=1182 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=82830#probl ...

  3. linux (centos 6.4)下编译安装git

    是时候动手尝试下 Git 了,不过得先安装好它.有许多种安装方式,主要分为两种,一种是通过编译源代码来安装:另一种是使用为特定平台预编译好的安装包(yum install git). 若是条件允许,从 ...

  4. POJ3273 Monthly Expense 2017-05-11 18:02 30人阅读 评论(0) 收藏

    Monthly Expense Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 25959   Accepted: 10021 ...

  5. 1028. Hanoi Tower Sequence

    1028. Hanoi Tower Sequence Constraints Time Limit: 1 secs, Memory Limit: 32 MB Description Hanoi Tow ...

  6. AXI总线(转)

    AXI(Advanced eXtensible Interface)是一种总协议,该协议是ARM公司提出的AMBA(Advanced Microcontroller Bus Architecture) ...

  7. 大猪蹄子队 Scrum meeting 合集

    大猪蹄子队 Scrum meeting 合集 18-10-29 第一天 http://note.youdao.com/noteshare?id=79ce5a89f85995ef68deae14e538 ...

  8. IDEA13 SVN配置

    这个算是解决了,idea13是支持svn 1.8. 步骤: 1.下载svn客户端软件,小乌龟:TortoiseSVN.安装的时候,一定要选择安装svn命令行的那个选项.当前版本1.8默认只会忽略命令行 ...

  9. 为某金融企业的IT技术部人员提供基于TFS的软件研发流程介绍

    受莫金融企业IT信息技术部的邀请,为该金融企业的某省分公司.地市分公司的IT技术人员提供了一场基于TFS的软件研发流程的技术培训,希望可以借此提高该企业的软件研发.运维水平,同时推动企业软件研发信息化 ...

  10. linux系统编程之进程(三):进程复制fork,孤儿进程,僵尸进程

    本节目标: 复制进程映像 fork系统调用 孤儿进程.僵尸进程 写时复制 一,进程复制(或产生)      使用fork函数得到的子进程从父进程的继承了整个进程的地址空间,包括:进程上下文.进程堆栈. ...