2

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.

Holger
  • 285,553
  • 42
  • 434
  • 765
mbo
  • 21
  • 2
  • 2
    While it is possible to retrieve the constant value without an instance, a construct like `Test.this.i` reference a nonexistent `this` instance should not compile, for sure, and it doesn’t with `javac`. So I’d consider it an Eclipse bug. – Holger Jan 14 '20 at 07:56
  • Yes, you are probably right, I tested with IntelliJ and indeed it did non compile... I let the question open anyway. I keep thinking the JLS contains something more or less related to this. Maybe the bug in Eclipse is caused by a wrong interpretation of that part in the specification. Thank you ! – mbo Jan 15 '20 at 00:15
  • 1
    Yes, it’s a good idea to keep the question open. I tagged it with `[eclipse]` to draw the attention of the right people to it… – Holger Jan 15 '20 at 08:29

0 Answers0