我需要解释为什么以下代码无法编译(就范围和生命周期而言):
class ClassInMethod
{
public static void main(String[] args)
{
int local = 1;
class Inner
{
public void method()
{
System.out.println(local);
}
}
}
}
我认为这是因为:任何使用但未在内部类中声明的局部变量都必须声明为“final”。因此,在本例中,'local' 必须声明为 final,因为它的作用域和生命周期在 main 方法中结束(因此需要更改为:final int local = 1;)。
还有其他建议吗?