这种情况一般发生在“在静态方法里面使用内部类”

测试代码:

public class Test {public static void main(String[] args) {A a = new A(1);}class A {int x;public A() {}public A(int x) {this.x = x;}}
}

  上面这份代码在main函数里面会发生编译错误,为了解决问题,让我们先看看编译器的提示:

编译器告诉我们,没有可访问的Test类的实例,意味着下面将要使用实例,那个地方使用了呢?看后面的括号里面,"x.new A()",new A()实际上是x.new A(),其中x是Test的一个实例。所以我们可以先创建Test的一个实例,然后用这个实例的new方法new一个A()出来。

于是可以改成下面的代码:

public class Test {public static void main(String[] args) {Test test = new Test();A a = test.new A(1);}class A {int x;public A() {}public A(int x) {this.x = x;}}
}

  明白了这一点就可以解释下面的代码了:

public class Test {public static void main(String[] args) {Test test = new Test();A a = test.new A(1);B b = new B(2);//这里的B不是一个内部类,所以不存在上述问题所以可以直接写newA.AA aa = a.new AA(3);}class A {int x;public A() {}public A(int x) {this.x = x;}class AA {int x;AA() {}AA(int x) {this.x = x;}}}
}class B {int x;public B() {}public B(int x) {this.x = x;}
}

或者用令外一种方法,将内部类改成静态的:

public class Test {public static void main(String[] args) {A a = new A();}static class A {int x;public A() {}public A(int x) {this.x = x;}}
}

  

转载于:https://www.cnblogs.com/jklongint/p/4977230.html

java中no enclosing instance of type * is accessible的解决方法相关推荐

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

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

  2. Java中url传递中文参数取值乱码的解决方法

    Java中url传递中文参数取值乱码的解决方法 参考文章: (1)Java中url传递中文参数取值乱码的解决方法 (2)https://www.cnblogs.com/liwenjuan/p/3211 ...

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

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

  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. No enclosing instance of type * is accessible. Must qualify the allocation with an enclosing instanc

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

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

    报错的完整信息: No enclosing instance of type OutterClass is accessible. Must qualify the allocation with a ...

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

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

  8. No enclosing instance of type E is accessible. Must qualify the allocation with an enclosing instanc

    java : No enclosing instance of type E is accessible. Must qualify the allocation with an enclosing ...

  9. Java中出现No enclosing instance of type Demo01 is accessible解决方法参考

    出现类似以下错误 No enclosing instance of type Demo01 is accessible. Must qualify the allocation with an enc ...

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

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

最新文章

  1. 零起点学算法10——求圆柱体的表面积
  2. 基于自编码器的表征学习:如何攻克半监督和无监督学习?
  3. 网络安全-XSS笔记
  4. 热电偶单片机代码c语言,基于STC12C5A60S2单片机的恒温箱设计-LCD1602-热电偶(电路图+程序源码)...
  5. 使用NW.js封装微信公众号菜单编辑器为桌面应用
  6. 两点之间直线只有一条,曲线却有无数条,哪条才是最短的?
  7. .NET BackgroundWorker的一般使用方式
  8. pythonopencv人脸相似度_图像相似度算法的个人见解(pythonopencv)-Go语言中文社区...
  9. Spring Boot 框架介绍和使用
  10. sql 字段很多怎么汇总_利用集算器实现分库汇总
  11. mac u盘装linux系统教程,mac上制作linux系统U盘安装盘
  12. 解决办法:char类型的实参与LPCWSTR类型的形参类型不兼容
  13. Linux系统压缩解压缩
  14. SpringBoot - @DependsOn注解详解
  15. 「案例分析」生鲜行业B2B供应链平台开发案例
  16. Massive MIMO
  17. 贝叶斯算法(bayesian)在反垃圾邮件中的应用
  18. 视觉推理(Visual Reasoning)
  19. 数码相机闪光灯存储卡挑选篇
  20. 微程序与微指令和微命令

热门文章

  1. 国庆假期程序员是这样给自己粉饰无限的逼格!
  2. shell基础之更改IP
  3. 关于王者荣耀防沉迷以及各种实名认证
  4. Java集合框架类图
  5. mysql 主命令总结
  6. 《玩转D语言系列》三、轻松大跃进,把它当C语言先用起来
  7. 编程菜鸟的日记-初学尝试编程递归
  8. MFC浮动窗口使用方法和注意事项
  9. [Usaco2009 Feb]Revamping Trails 堆优化 Dijkstra
  10. HDU 3966 Aragorn's Story(树链剖分)题解