I need to explain why the following code would fail to compile (in terms of scope and lifetime):
class ClassInMethod
{
public static void main(String[] args)
{
int local = 1;
class Inner
{
public void method()
{
System.out.println(local);
}
}
}
}
I think it's because: Any local variable used but not declared in an inner class must be declared ‘final’. Thus, in this example ‘local’ must be declared final because its scope and lifetime end within the main method (so needs to be changed to: final int local = 1;).
Any other suggestions?