No enclosing instance of type test is accessible. Must qualify the allocation with an enclosing inst
今日遇到一个报错如下:

No enclosing instance of type test is accessible. Must qualify the allocation with an enclosing instance of type test (e.g. x.new A() where x is an instance of test).
问题出现的原因是:
因为在做Demo测试,我写的内部类是动态的,即public class开头无static关键字修饰,而测试主程序是静态的main方法。在Java中,类中的静态方法不能直接调用动态方法。只有将某个内部类修饰为静态类,才可以再静态类中调用带类的成员变量和成员方法。
为什么静态方法不能直接访问非静态成员呢?
静态成员是在JVM的ClassLoader加载类的时候初始化的,而非静态成员是在创建对象,也就是new操作的时候才初始化的。类加载的时候初始化的静态成员已经分配内存空间,所以可以访问,而非静态成员还没有通过new创建对象而进行初始化,所有当然不能访问了。
解决方案:
1.将动态内部类改为静态(public static class)。
2.将内部类改为外部类。
3.别用静态main方法调用。
在class外不用加static
而在class内要加不然就报错。
No enclosing instance of type test is accessible. Must qualify the allocation with an enclosing inst的更多相关文章
- 【转】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 ...
- 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 test8 is accessible. Must qualify the allocation with an enclosing instance of type test8 (e.g. x.new A() where x is an
在编译一个例子时,结果编译时出现: No enclosing instance of type test8 is accessible. Must qualify the allocation wit ...
- No enclosing instance of type Outer is accessible. Must qualify the allocation with an enclosing instance of type Outer (e.g. x.new A() where x is an instance of Outer)
之前看内部类的时候没发现这个问题,今天写代码的时候遇到,写个最简单的例子: 下面这一段代码 红色的部分就是编译报错: No enclosing instance of type Outer is ac ...
- No enclosing instance of type Test is accessible. Must qualify the allocation with an enclosing instance of type Test (e.g. x.new A() where x is an instance of Test).
Java编写代码过程中遇到了一个问题,main方法中创建内部类的实例时,编译阶段出现错误,查看错误描述: No enclosing instance of type Test is accessibl ...
- No enclosing instance of type Demo is accessible. Must qualify the allocation with an enclosing instance of type Demo (e.g. x.new A() where x is an instance of Demo).
No enclosing instance of type Demo is accessible. Must qualify the allocation with an enclosing inst ...
- 【eclipse】No enclosing instance of type A is accessible. Must qualify the allocation with an enclosing instance of type A
用 eclipse 写 Java 代码时出现了这个问题,详细如下: No enclosing instance of type TestParsingLinkedList is accessible. ...
- No enclosing instance of type E is accessible. Must qualify the allocation with an enclosing
在Java中,类中的静态方法不能直接调用动态方法.只有将某个内部类修饰为静态类,然后才能够在静态类中调用该类的成员变量与成员方法.所以在不做其他变动的情况下,最简单的解决办法是将public clas ...
- Java中报错No enclosing instance of type caiquan is accessible. Must qualify the allocation with an enclosing instance of type caiquan (e.g. x.new A() where x is an instance of caiquan).
package test;import java.util.Scanner;import java.util.Random;public class caiquan { public static v ...
随机推荐
- 本地VMware虚拟机环境CentOS7.6 安装宝塔Linux面板
之前测试一直都在使用phpenv和phpstudy ,不过这两个集成环境时长容易出bug,各种问题劝退.之前Windows开始Linux的体验又不尽人意,今天介绍一个别的方法.VMware安装Linu ...
- 非阻塞IO与异步IO
一.非阻塞IO的轮询读写 ---如果当前进程有多个输入终端和多个输出终端呢?while((n=read(STDIN_FILENO,buf,buf_size))>0){ if(write(S ...
- gym 101911
A. Coffee Break 题意:每天有m小时,你喝咖啡需要花一小时,你想在n个时刻都喝过一次咖啡,老板规定连续喝咖啡的间隔必须是d以上,求最少需要多少天才能喝够n次咖啡,并输出每个时刻第几天喝. ...
- 编写第一个JavaScript程序
编写第一个程序 在 HTML 页面中嵌入 JavaScript 脚本需要使用 <script> 标签,用户可以在 <script> 标签中直接编写 JavaScript 代码, ...
- greenplum 导入数据方式
参考: http://gpdb.docs.pivotal.io/4390/admin_guide/load/topics/g-working-with-file-based-ext-tables.ht ...
- python merge、join、concat用法与区别
由于合并变化较大,以后函数可能会修改,只给出一些例子作为参考 总结: merge.join 1.当没有索引时:merge.join为按照一定条件合并 2.当有索引.并按照索引合并时,得到结果为两者混 ...
- 第1节 网站点击流项目(上):4、网站的数据采集,使用flume的taildir实现多个文件的监控采集
一. 模块开发----数据采集 1. 需求 在网站web流量日志分析这种场景中,对数据采集部分的可靠性.容错能力要求通常不会非常严苛,因此使用通用的flume日志采集框架完全可以满足需求. 2. Fl ...
- mybatis连接数据库出错获取不到SQLsession
采用mybatis连接数据库时候出现的问题描述: 数据库连接配置正确,mybatis-config数据库等部分配置均正确,连接数据库是OK的 <properties resource=" ...
- jsp快速回顾
http://www.cnblogs.com/zfc2201/archive/2011/08/17/2143615.html http://blog.163.com/mount_lee/blog/st ...
- 1、MYSQL 数据库的安装与配置
安装 1.打开官网https://www.mysql.com,选择社区版本 2.如图点击下在安装(本人在下载过程中亲身感觉下载时间非常漫长,需要等待,不知道为啥会有限速,可以参考网上教程用迅雷进行 ...