Here is a question from the Exam Cram by B.Brogden.
In the following code for a class in which methoda has an inner class, which variables would the statement in line 8 be able to use in place of XX? (Check all correct answers)
public class Base
{
private static final int ID = 3;
private
String name;
public void methodA(final int nn)
{
int serialN = 11;
class Inner
{
void showResult()
{
System.out.println("ResultID = " + XX);
}
}
new Inner().showResult();
}
}
a.The int ID in line 2
b.The String in line 3
c.The int nn in line 4
d.The int serialN in line 5
The answers are a, b and c.
According to exam cram: "Local inner classes and anonymous classes can refer to local variables only if they are declared final.". Answer a and c fulfill this rule. However, why does it allow to access to the String in line 3? Please explain.