No enclosing instance of type test5 is accessible. Must qualify the allocation with an enclosing instance of type test5 (e.g. x.new A() where x is an instance of test5).
在一个类里面的main函数,经常会写一些测试代码。
在调用这个类里面的内部类的时候,出现不能初始化内部类的对象实例。
发现两种解决方法:
1、把内部类换成静态类
2、直接把在main函数初始化的内部类,挪到类里面的一个方法里面即可。
原先出问题的代码
public class W extends Wifi<String>
{
@Override
public String getNet()
{
// TODO Auto-generated method stub
return "WWWW";
}
}
public static void main(String[] args)
{
// TODO Auto-generated method stub
W ww = new W();
ww.net();
ww.ondo();
System.out.println(ww.getNet());
}
修改之后:
public class W extends Wifi<String>
{
@Override
public String getNet()
{
// TODO Auto-generated method stub
return "WWWW";
}
}
public void testW()
{
W ww = new W();
ww.net();
ww.ondo();
System.out.println(ww.getNet());
}
public static void main(String[] args)
{
// TODO Auto-generated method stub
test5 test5 = new test5();
test5.testW();
}
本文介绍了在Java中遇到的内部类初始化错误“No enclosing instance of type test5 is accessible”,并提供了两种解决方案:一是将内部类改为静态类;二是将内部类实例化的代码移至类的方法内。
334

被折叠的 条评论
为什么被折叠?



