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的时候,碰到这个异常,导致Eclipse报错。
package com.frank;
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
A a = new A();
}
class A{
}
}
当时没有注意,仔细想了想,也是合理的。main是Test类的static方法,按照常理它只能访问Test类中static资源,而class A是非static所以报错了。解决方法很简单,给class A添加static属性就可以了。
package com.frank;
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
A a = new A();
}
static class A{
}
}
当然还有另外一种方式,没有必要把A放在Test中做内部类,只要把它移到Test外部地方定义也可以解决这个问题。
package com.frank;
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
A a = new A();
}
}
class A{
}
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有