Consider the following set of expressions:
class T {{
/*1*/ Object o = T.super; // error: '.' expected
/*2*/ o.toString();
}}
An attempt to compile this will fail on line /*1*/
with the error:
error: '.' expected
o = T.super;
^
both when using OpenJDK 1.8.0 (Ubuntu) or Oracle JDK 1.8 (Windows).
However, Eclipse 4.5.0 (Mars) compiles this without any error and it results in:
class T {
T();
0 aload_0 [this]
1 invokespecial java.lang.Object() [8] // super()
4 aload_0 [this]
5 astore_1 [o] // o = T.super
7 invokevirtual java.lang.Object.toString() : java.lang.String [10]
10 pop // ^-- o.toString()
11 return
}
From this you can see that the line /*1*/
of the java code (line 5
of the result) correctly stores this
casted as Object
(Eclipse's understanding of T.super
) into the local variable o
. When the code is executed, it completes normally and the line /*2*/
produces a correct result.
So far I failed to find anything relevant to o = T.super;
in the Java 8 Language Specification, i.e. whether it's legal or not. Since it doesn't explicitly state that it's a legal expression, my guess is that it means it's illegal. But then, why Eclipse considers it legal? Hence my question:
Is T.super
a legal expression as per JLS?
Edit: Simplified the code by removing a wrapping inner class.