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 accessible. Must qualify the allocation with an enclosing instance of type Test (e.g. x.new A() where x is an instance of Test).
经过查证,是因为内部类是动态的(无static关键字修饰),而main方法是静态的,普通的内部类对象隐含地保存了一个引用,指向创建它的外围类对象,所以要在static方法(类加载时已经初始化)调用内部类的必须先创建外部类。即应该这样创建“DanymicTest test = new StaticCallDynamic().new DanymicTest();”其中StaticCallDynamic为外部类,DanymicTest为内部动态类;如果将内部内修改为静态类,可以在main中直接创建内部类实例。
顺便看了下静态方法对动态方法的调用,如果在静态方法中直接调用静态方法,其中print()为类中的动态方法,会出现如下问题:
Cannot make a static reference to the non-static method print() from the type StaticCallDynamic
这个要从java的内存机制去分析,首先当你New 一个对象的时候,并不是先在堆中为对象开辟内存空间,而是先将类中的静态方法(带有static修饰的静态函数)的代码加载到一个叫做方法区的地方,然后再在堆内存中创建对象。所以说静态方法会随着类的加载而被加载。当你new一个对象时,该对象存在于对内存中,this关键字一般指该对象,但是如果没有new对象,而是通过类名调用该类的静态方法也可以。
程序最终都是在内存中执行,变量只有在内存中占有一席之地时才会被访问,类的静态成员(变态和方法)属于类本身,在类加载的时候就会分配内存,可以通过类名直接去访问,非静态成员(变量和方法)属于类的对象,所以只有在类的对象产生(创建实例)的时候才会分配内存,然后通过类的对象去访问。在一个类的静态成员中去访问非静态成员之所以会出错是因为在类的非静态成员不存在的时候静态成员就已经存在了,访问一个内存中不存在的东西当然会出错。
就类中的静态方法不能调用动态方法,所以要么将动态方法也改为静态方法(static),要么通过创建该静态方法所在的实例(new 静态c)再通过实例(c.d动态)来调用。
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出现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 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 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 en ...
- 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 ...
随机推荐
- Java 8 特性 —— 默认方法和静态方法
Java 8 新增了接口的默认方法.简单说,默认方法就是接口可以有实现方法,而且不需要实现类去实现其方法.我们只需在方法名前面加个 default 关键字即可实现默认方法. 为什么要有这个特性?之前的 ...
- requests爆破+简单数字验证码
验证码用正则获取 然后提交:访问方式为get:由于有验证码 要用session:注意:获取验证码的时候所打开的网页要用当前session(即要get的) 不能另外打开(如urllib中的打开网页)网页 ...
- C#面向对象基本概念总结
快过年了,发一篇自己的复习总结.以下内容均是个人理解,如文章有幸被浏览,如有错误的地方欢迎大家提出,相互学习相互进步! 面向对象三大基本特征:封装,继承,多态 一.类 (对象声明的三种方式:以普通基类 ...
- ECS集群管理docker
1. create ECR Repositories on AWS a. local server need install git/docker/awscli b. config aws: use ...
- my live thinkcenter / ThinkCentre M920x Tiny / Thinkpad yoga 12 vPro
s 025-58816312 联想3C服务中心:栖霞区学海路鸿运家园1栋6室 / 珠江路华海大厦8楼联想服务中心 营业时间:周一至周日,9:00∼18:00 ThinkPad Yoga 12 i7 v ...
- CDN-常用静态资源公共库
js.css 等静态资源可以放在自己的服务器上,或者引用网络上的公共库使用(CDN) CDN 读取失败时从本地加载 <script src="https://code.jquery.c ...
- Python——错误笔记
1.同一个项目(Project)下,不同的库(Package)之间默认不相互引用,注意这样的错误: 2.使用sklearn之前,需要安装scipy库
- C++编译/运行过程中产生的各种文件
参考:https://blog.csdn.net/weixin_39609623/article/details/82348576 Windows中的obj,lib,dll,exe文件 lib是和dl ...
- 一道Python面试题:给出d = [True, False, True, False, True],请利用列表d,只用一句话返回列表[0,2,4]
看题:给出d = [True, False, True, False, True],请利用列表d,只用一句话返回列表[0,2,4] 这道题的关键是拿到True的索引值,最初我是用list的index方 ...
- windows安装解压版mysql
记录下用批处理安装mysql5.7.18的过程与踩到的坑 先在安装目录新建文件my.ini [mysql] default-character-set=utf8 basedir=TODO datadi ...