class Outer
{
int outer_x=100;
void test()
{
for(int i=0;i<10;i++)
{
class Inner
{
void display()
{
System.out.println("display:outer_x"+outer_x);
}
}
}
Inner inner=new Inner();//cannot resolve symbol class Inner
inner.display();
}
}
class InnerClassDemo
{
public static void main(String []args)
{
Outer outer=new Outer();
outer.test();
}
}
这样运行出现错误:cannot resolve symbol class Inner
然后把 Inner inner=new Inner();
inner.display();
放到for循环内,就没这个错误了:
class Outer
{
int outer_x=100;
void test()
{
for(int i=0;i<10;i++)
{
class Inner
{
void display()
{
System.out.println("display:outer_x"+outer_x);
}
}
Inner inner=new Inner();
inner.display();
}
}
}
class InnerClassDemo
{
public static void main(String []args)
{
Outer outer=new Outer();
outer.test();
}
}
这是为什么呢???