Probably missing "or are effectively final" in Example 8.1.3-2.

Jan Kurnatowski jwk at onet.com.pl
Fri Dec 9 22:00:49 UTC 2016


Hello,
in https://docs.oracle.com/javase/specs/jls/se8/html/jls-8.html#jls-8.1.3 in "Example 8.1.3-2. Inner Class Declarations":
"class Outer {
int i = 100;
static void classMethod() {
final int l = 200;
class LocalInStaticContext {
int k = i; // Compile-time error
int m = l; // OK
}
}
void foo() {
class Local { // A local class
int j = i;
}
}
}
The declaration of class LocalInStaticContext occurs in a static context due to being within the static method classMethod. Instance variables of class Outer are not available within the body of a static method. In particular, instance variables of Outer are not available inside the body of LocalInStaticContext. However, local variables from the surrounding method may be referred to without error (provided they are marked final)."
the last sentence should probably also include information about local variables that are effectively final, for example:
"However, local variables from the surrounding method may be referred to without error (provided they are declared final or are effectively final)."
I conclude this from the following sentence in https://docs.oracle.com/javase/specs/jls/se8/html/jls-8.html#jls-8.1.3 :
"Any local variable, formal parameter, or exception parameter used but not declared in an inner class must either be declared final or be effectively final (§4.12.4), or a compile-time error occurs where the use is attempted."
For testing purposes I also changed the line "final int l = 200;" to "int l = 200;" and removed the line "int k = i; // Compile-time error" in the aforementioned code, and compiled it with Oracle Java 1.8.0_111 compiler - there was no compile-time error in the line "int m = l; // OK" after this modification.
Regards,
Jan Kurnatowski


More information about the jls-jvms-spec-comments mailing list