Must qualify the allocation with an enclosing instance of type AAA (e.g. x.new A() where x
来源:互联网 发布:手机淘宝店的店招尺寸 编辑:程序博客网 时间:2024/05/21 14:19
今天在写一个程序的时候在一个类中写了内部类。在main()中进行了实现,
但是在编译的时候进行了报错。自己也不知道错在哪里就出现了这样的英文。
Exception in thread “main” java.lang.Error: Unresolved compilation problem:
No enclosing instance of type AAA is accessible. Must qualify the allocation with an enclosing instance of type AAA (e.g. x.new A() where x is an instance of AAA).
at a.AAA.main(AAA.java:24)
如图:
后来发现是这样的原因:
在实例化一个类的时候只能对在类中的进行实例化,
例如代码:
package a;
public class AAA {
int a;
public AAA(int m){
this.a = m;
System.out.println(“可以输出a的值是:\t” + a);
}
class BBB{
int b;
public BBB(int n){
this.b = n;
System.out.println(“可以输出b的值是: \t” + b);
}
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
AAA aa = new AAA(100);
BBB bb = new BBB(50);//这里出现的错误,因为BBB内部类在AAA类中,因此实例化应该在AAA构造方法或者AAA内部进行实例化。
//或者吧class改成static class BBB{………………..}.
//具体这里为什么要改成static class。是因为这样的话就成了静态内部类。
}
}
再找资料的同事还发现一种情况。
根据提示,没有可访问的内部类E的实例,必须分配一个合适的内部类E的实例(如x.new A(),x必须是E的实例。)看着这句提示,我就纳闷了,我已经用new实例化了这个类,为什么还不行呢。
于是百度谷歌了一下相关资料。原来我写的内部类是动态的,也就是开头以public class开头。而主程序是public static class main。在Java中,类中的静态方法不能直接调用动态方法。只有将某个内部类修饰为静态类,然后才能够在静态类中调用该类的成员变量与成员方法。所以在不做其他变动的情况下,最简单的解决办法是将public class改为public static class。
希望对各位有所帮助。
- Must qualify the allocation with an enclosing instance of type AAA (e.g. x.new A() where x
- java编译时出现Must qualify the allocation with an enclosing instance of type AAA (e.g. x.new A() where x
- No enclosing instance of type X is accessible. Must qualify the allocation with an enclosing
- No enclosing instance of type X is accessible. Must qualify the allocation with an enclosing instanc
- No enclosing instance of type E is accessible. Must qualify the allocation with an enclosing instanc
- No enclosing instance of type E is accessible. Must qualify the allocation with an enclosing
- No enclosing instance of type E is accessible. Must qualify the allocation with an enclosing
- No enclosing instance of type E is accessible. Must qualify the allocation with an enclosing
- No enclosing instance of type E is accessible. Must qualify the allocation with an enclosing instanc
- No enclosing instance of type E is accessible. Must qualify the allocation with an enclosing insta
- No enclosing instance of type E is accessible. Must qualify the allocation with an enclosing instanc
- No enclosing instance of type E is accessible. Must qualify the allocation with an enclosing instanc
- No enclosing instance of type E is accessible. Must qualify the allocation with an enclosing instanc
- No enclosing instance of type E is accessible. Must qualify the allocation with an en
- [Error]No enclosing instance of type XX is accessible. Must qualify the allocation with an e
- No enclosing instance of type A is accessible. Must qualify the allocation with an enclosin
- 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
- 程序设计进阶 编程题#1:寻找下标
- android界面之美---自定义网络请求进度加载对话框
- 每天一个linux命令(1):ls命令
- js对table常用的操作(学习分享一)
- 上机实例之vector内存分配
- Must qualify the allocation with an enclosing instance of type AAA (e.g. x.new A() where x
- TCP三次握手四次挥手过程详解
- 【置顶】我的/etc/profile
- APP接口自动化测试JAVA+TestNG之HTTP接口测试实例
- [SCOI2005]互不侵犯King
- C# Word中设置/更改文本方向
- 数据库基本操作
- iOS Button 按钮 设置图片在文字的右边
- OC-NSURLConnection-同步get、异步get、同步post、异步post