报错:No enclosing instance of type ExtendsDemo is accessible.
来源:互联网 发布:手机域名在哪里看 编辑:程序博客网 时间:2024/06/05 06:54
英文提示报错:No enclosing instance of type ExtendsDemo is accessible. Must qualify the allocation with an enclosing instance of type ExtendsDemo (e.g. x.new A() where x is an instance of ExtendsDemo).
中文翻译:没有可访问的内部类ExtendsDemo的实例,必须分配一个合适的内部类ExtendsDemo的实例(如x.new A(),x必须是ExtendsDemo的实例。)
可是我已经在main方法里Student stu = new Student();//此处Student为我写的内部类
为什么捏?我明明已经实例化了啊?
原因:原来我写的内部类是非静态的,也就是没有static修饰的。而我通过public static class main方法调用,main是有static修饰的。在Java中,类中的静态方法不能直接调用非静态方法。然后我就在内部类处加上了static,然后就不报错了。
那么问题又来了。。。此处 new Student()是调用的构造方法啊,难道构造方法是静态的?然后我就去找了资料,关于构造方法是否为静态类解释如下:
《java编程思想》中文第四版96页:
总结一下对象的创建过程,假设有个名为Dog的类:
1.即使没有显示地使用static关键字,构造器实际上也是静态方法。因此,当首次创建类型为Dog的对象时(构造器可以看成
是静态方法),或者Dog类的静态方法/静态域首次被访问时,Java解释器必须查找类路径,以定位Dog.class文件
回到最初的问题,解决方案:将非静态内部类改为静态内部类,即把public class改为public static class.
- 报错:No enclosing instance of type ExtendsDemo is accessible.
- No enclosing instance of type * is accessible.
- No enclosing instance of type is accessible.
- 关于No enclosing instance of type MyProject is accessible的报错
- JAVA编译报错-No enclosing instance of type Test 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
- No enclosing instance of type ThreadTest is accessible.错误解决
- 【JAVA001】No enclosing instance of type XXX is accessible.
- No enclosing instance of type E is accessible
- No enclosing instance of type E is accessible
- 黑马程序员--No enclosing instance of type E is accessible.
- No enclosing instance of type xxx is accessible
- No enclosing instance of type E is accessible
- No enclosing instance of type Demo06 is accessible.
- No enclosing instance of type XXX is accessible
- No enclosing instance of the type RegisterActivity is accessible
- No enclosing instance of type test is accessible.
- Leetcode 刷题: Q344 Reverse String
- Laravel框架
- Linux CentOS 7 下通过Memcached实现Session共享
- Python 中re模块学习(转)
- PL/SQL存储过程调试
- 报错:No enclosing instance of type ExtendsDemo is accessible.
- 朴素贝叶斯
- Android Touch事件传递机制通俗讲解
- 里氏替换原则
- scala学习笔记————如何实现类里的只读属性
- 网络制式(Duplex Mode)
- 使用xUtils框架实现多线程下载和断点续传
- mysql group_cocnat()最大限制
- 编码惯例