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

编程入门 行业动态 更新时间:2024-10-07 00:17:43

No enclosing instance of <a href=https://www.elefans.com/category/jswz/34/1769883.html style=type StudentTest is accessible. Must qualify the allocation with an enclosi"/>

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

No enclosing instance of type StudentTest is accessible

原因:主程序是静态,局部类是动态的,类中的静态方法不能直接调用动态方法;

解决方案:1、可以将局部类改为静态类

                2、将局部类移除静态类中

注:静态成员是在JVM的ClassLoader加载类的时候初始化的,而非静态成员是在创建对象,也就是new操作的时候才初始化的。类加载的时候初始化static的成员,此时static 已经分配内存空间,所以可以访问,非static的成员还没有通过new创建对象而进行初始化,所以必然不可以访问

静态的使用注意事项:

1.静态方法只能访问静态成员(包括成员变量和成员方法), 非静态方法可以访问静态也可以访问非静态(为什么?有时需要创建对象调用方法,有时可以直接调用?:对于静态方法,其他静态方法或者非静态都可以直接调用,而对于非静态方法,其他的非静态方法可以直接调用它,但是其他静态方法,只有用过对象才能调用它)

2.静态方法中不可以定义this,super关键字,因为 一个类中,一个static变量只会有一个内存空间,虽然有多个类实例,但这些类实例中的这个static变量会共享同一个内存空间。静态方法在优先于对象存在,所以静态方法中不可以出现this,super关键字。

更多推荐

No enclosing instance of type StudentTest is accessible. Must qualify the alloca

本文发布于:2024-02-07 06:24:19,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1753833.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:type   StudentTest   enclosing   instance   allocation

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!