Java 报错No enclosing instance of type E is accessible. Must qualify the allocation with an enclosing
来源:互联网 发布:maka软件官网 编辑:程序博客网 时间:2024/06/04 19:47
public class EqualDemo { class Person { private String idcard; private String name; public Person(String idcard, String name) { this.idcard = idcard; this.name = name; } } public static void main(String[] args) { Person person = new Person("", "");//Error: No enclosing instance of type EqualDemo is accessible. Must qualify the allocation with an enclosing instance of type EqualDemo (e.g. x.new A() where x is an instance of EqualDemo). System.out.println(); }}
解决方案:在Person内部类前面添加static,将内部类声明为静态内部类。
原因如下:在Java中
(1)类的静态方法不能直接调用动态方法。
(2)类的静态方法不能初始化内部类(包括该类的内部类,和其他文件中的内部类)。
(3)类的静态方法可以初始化其他类文件中的类。
(4)类的静态方法可以初始化静态类(包括静态内部类)。
我看了网上很多种解释方案,发现全都是错误的。可笑的是竟然还很多人点赞。。。
真正的原因是:对象的生命周期。
(1)静态变量和静态方法的生命周期
静态变量和静态方法,在进程创建的时候就被创建了,而不是在对象创建的时候才被创建,并且其生命周期同该进程的生命周期一样长,其占用的系统资源是初始化进程申请资源的一部分。
(2)内部类的生命周期
如果是非静态的内部类,必须由一个已创建的实例去创建一个对应的内部类实例,才能开始其生命周期,但如果是静态的内部类,则可以直接创建。因为所有的静态对象,都在线程创建的时候就一起被创建了。
了解了这些以后,上面的问题自然就可以解释的通了。
(1)静态方法在线程初始化的时候就被创建了,而这时候普通类都还没来的及初始化,因此不能调用它的内部类。
(2)而如果是静态类,记住一个原则——变量优先于方法被创建。因此静态方法自然就可以调用静态对象了。
(3)之所以可以在静态方法中调用普通类是因为我们可以手动初始化该普通类。
1 0
- 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 E is accessible. Must qualify the allocation with an enclosing instanc
- No enclosing instance of type E is accessible. Must qualify the allocation with an enclosing
- No enclosing instance of type E is accessible. Must qualify the allocation with an enclosing
- No enclosing instance of type E is accessible. Must qualify the allocation with an enclosing
- No enclosing instance of type E is accessible. Must qualify the allocation with an enclosing instanc
- No enclosing instance of type E is accessible. Must qualify the allocation with an enclosing insta
- No enclosing instance of type E is accessible. Must qualify the allocation with an enclosing instanc
- No enclosing instance of type E is accessible. Must qualify the allocation with an enclosing instanc
- 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
- 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
- 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
- ITjob HTML5/企业网站建设不可忽视的六大问题
- IL2CPP 深入讲解:垃圾回收器的集成(六)
- ajax 设置Access-Control-Allow-Origin实现跨域访问
- 第二周 项目3.2 体验复杂度(汉诺塔)
- android:用getIdentifier()获取资源Id
- Java 报错No enclosing instance of type E is accessible. Must qualify the allocation with an enclosing
- 多文件组织的文件
- 对乡村地名进行模糊匹配
- 让用户信息安全起来
- MySQL创建用户与授权方法
- IOS开发中常用的一些宏定义
- MyBatis 插入MySQL数据库返回主键异常 java.lang.Integer cannot be cast to java.lang.Long
- java练习---计算平均分和总成绩
- Linux kernel-<armv7a-switch_to>