Playing with the special case of Inner classes which have an enclosing block instead of a lexically enclosing instance (Java Language Specification §8.1.3), I wrote following example:
public class Test {
private static final Object o = new Object() {
@Override
public String toString() {
return "" + Test.this.i; // Test.this does not exist
}
};
public static void main(final String[] args) {
System.out.println(Test.o);
}
final int i = 1;
}
This code compiles and runs, producing the following output :
1
The strange thing is that the toString()
method of o
refers to an enclosing instance of Test
which does not exist. There is no instance of Test
created in this sample.
I guess that this is because the field i
, being final and having an initializer, is considered as static. The compiler knows this and probably that the value of the field is inlined.
But there must be an explanation of this behaviour in the Java Language Specification. I think I read it once, but I couldn't find it anymore.
Could someone tell where this is explained in the JLS ? Thank you.