报错的完整信息:

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

错误的出现背景:

Java

在类中添加内部类,并在main函数中通过语句:

OutterClass.InnerClass innerClass = new OutterClass.InnerClass();

试图构造内部类对象时出现。

完整结构如下:

public class OutterClass {class InnerClass {void print() {System.out.println("this is innerClass");}}public static void main(String[] args) {OutterClass.InnerClass innerClass = new OutterClass.InnerClass();innerClass.print();}}

思考:

首先对报错信息进行理解,粗略翻译如下:“没有外部类的实例可以访问,必须用外部类型的实例限定内存分配(例如:x.new A()x是一个外部类类型的实例”。

根据如上信息不难找到修改方法,我们需要一个OutterClass实例outterClass,有了这个实例才能实现对InnerClass的实例化,修改代码如下:

public static void main(String[] args) {OutterClass outterClass = new OutterClass();OutterClass.InnerClass innerClass = outterClass.new InnerClass();innerClass.print();}

参考各方面资料,对此进行解释:

众所周知,内部类是可以访问到外部类中的所有元素的,这一实现是通过在创建内部类实例时秘密捕获一个外部类实例的引用完成的。也就是说,一个内部类在实例化时必定需要依赖一个外部类的实例,所以内部类不可单独实例化存在。

拓展:

除此之外,还可以通过使用嵌套类避免该错误。

嵌套类就是声明为static的内部类,与需要在创建时隐秘保存一个创建它的外部类实例的引用的内部类不同,创建嵌套类不需要外部类的实例,但声明为static也意味着嵌套类只能访问外部类的static元素。

至于如何设计,使用内部类还是嵌套类就需要根据实际情况考虑了。

关于“No enclosing instance of type * is accessible. Must qualify the allocation with an enclo……”的总结与思考相关推荐

  1. No enclosing instance of type * is accessible. Must qualify the allocation with an enclosing instanc

    今天在一位大神的博客中学习多线程的知识,其中有一段关于线程中sleep()方法的测试代码,内容如下: public class SleepLockTest{ private static Object ...

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

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

  3. No enclosing instance of type TextRunnable is accessible. Must qualify the allocation with an enclo

    今天写demo的时候,遇到一个问题,关于内部类的,这里写一下 错误信息:No enclosing instance of type TextRunnable is accessible. Must q ...

  4. [Java]Error:No enclosing instance of type E is accessible. Must qualify the allocation with an enclo

    在自己试着写一个链表的例子时,由于参考的代码只有一部分,于是就自己补了一个内部类.结果编译时出现:No enclosing instance of type E is accessible. Must ...

  5. enclosing type java_java中no enclosing instance of type * is accessible的解决方法

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

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

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

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

    最近研究LDA源代码时,里面涉及到Comparable方法的使用.以前用过这个排序方法,现在想回顾一下.以下是程序,感觉没问题啊,结果报错了: Exception in thread "ma ...

  8. Java变异出现错误:No enclosing instance of type XXX is accessible

    摘要:写java代码时遇到下面的编译错误. 本文分享自华为云社区<Java中出现No enclosing instance of type XXX is accessible问题>,作者: ...

  9. No enclosing instance of type FormDetailBean is accessible. Must qualify the allocation with an encl

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

最新文章

  1. linux笔记:压缩解压命令gzip,gunzip,tar,zip,unzip,bzip2,bunzip2
  2. JavaWeb 错误/异常时页面提示
  3. 孙鑫VC视频学习笔记一
  4. python 模块命名空间_如何在python模块中执行导入而不污染其命名空间?
  5. 将数据导入到已存在的excel文件中
  6. 论文浅尝 | 基于深度强化学习的远程监督数据集的降噪
  7. 编译和使用APUE的源码
  8. sql语句优化之一:尽量使用索引避免全表扫描
  9. 在CentOS7上安装配置Corosync高可用集群过程全记录
  10. Byobu:打造多任务的Terminal
  11. 9.2【彩色模型】-----基于Opencv实现-----把一幅RGB图转为CMY图
  12. Vue——知识体系总结
  13. 使用FFMPEG类库分离出多媒体文件中的音频码流
  14. jQuery.access的作用
  15. iOS最好用的弹出框
  16. leetcode36.有效的数独(中等)
  17. lnmp平台的简单基础搭建
  18. 智商情商哪个重要_智商情商哪个更重要 辩论赛
  19. State Threads 回调终结者(ST线程库)
  20. 公众号平台域名配置规则

热门文章

  1. 在ros中使用 RPLIDAR_A1 激光雷达 8000点/秒 的配置方法
  2. 全球及中国氢燃料电池汽车行业销售规模与运营前景展望报告2022版
  3. 内蒙古大学计算机学院周建涛,刘靖(内蒙古大学副教授)_百度百科
  4. matlab对于图像压缩,Matlab的图像压缩技术
  5. SQL数据库的组合查询和统计查询
  6. 项目介绍以及第一次面经
  7. Leapfrog Triejoin:最坏情况下的最优连接算法
  8. [软件工具][原创]加减乘除自动出题带答案不重复生成可代替加减法印章使用教程
  9. has no exported member
  10. GIRAFFEDET: A HEAVY-NECK PARADIGM FOR OBJECT DETECTION