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 ...
随机推荐
- python获取最大、最小值
1.获取数组极值,并返回索引 c = [-10,-5,0,5,3,10,15,-20,25] print c.index(min(c)) # 返回最小值 print c.index(max(c)) ...
- 【富文本、JS】将接口传来的全部纯URL替换为富文本插件能识别到的img标签
replaceURLWithImage (text) { var a = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0 ...
- 吴裕雄 Bootstrap 前端框架开发——Bootstrap 按钮:禁用状态
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- 《ES6标准入门》(阮一峰)--12.Symbol
1.概述 ES5 的对象属性名都是字符串,这容易造成属性名的冲突.比如,你使用了一个他人提供的对象,但又想为这个对象添加新的方法(mixin 模式),新方法的名字就有可能与现有方法产生冲突.如果有一种 ...
- IDEA配置Scala开发环境
1.下载scala sdk 到页面 https://www.scala-lang.org/download/2.10.5.html 下载sdk,大数据平台目前的scala版本是2.10.5,下载后解压 ...
- Oracle 的DBA考证
转自 :https://www.cnblogs.com/chunge2050/archive/2013/04/16/3023730.html 详细的了解了几天之后,总结起来就是oracle为DBA认证 ...
- Compile-kernel-module
Compile-kernel-module 1. 内核模块编程1.1 简介1.2 加载内核模块1.3 最简单的模块1.4 模块必要信息1.4.1 内核模块必须至少包含的头文件:1.4.2 内核模块必须 ...
- 008.CI4框架CodeIgniter, Controller控制器传输参数到View视图
01. 在CI4中输出VIEW视图,并且传入参数,代码如下: <?php namespace App\Controllers; class Home extends BaseController ...
- 0108 spring的申明式事务
背景 互联网的金融和电商行业,最关注数据库事务. 业务核心 说明 金融行业-金融产品金额 不允许发生错误 电商行业-商品交易金额,商品库存 不允许发生错误 面临的难点: 高并发下保证: 数据一致性,高 ...
- 数据结构第二版之(课后题)BF算法病毒感染检测
//vs2013下编译通过.换别的编译器自行补充头文件和修改源代码#include<iostream> #include<fstream> #include <strin ...