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
发布评论