RFR: 8194743: Compiler implementation for Statements before super() [v14]

Archie Cobbs acobbs at openjdk.org
Sat Sep 23 03:19:19 UTC 2023


On Sat, 23 Sep 2023 01:59:27 GMT, Chen Liang <liach at openjdk.org> wrote:

> Should we make pre-initialization local classes static to ease the compiler implementation for "regulated Constructors" in the future?

I could be missing something but I don't see a potential problem.

All of the actions that would be prohibited in regulated constructors will be prohibited in a pre-construction context, so the compiler should already be doing the right thing.

If I understand your question, the particular action you're worried about is "Construction of an inner class with this as an implicit enclosing instance".

But that can't happen in a pre-construction context - you can _declare_ a local class in a pre-construction context, you just can't _instantiate_ it.

Here's an example:

public class Test {

    public int x;

    public Test() {
        class Local {
            { System.out.println(x); }
        }
        //new Local();     // would be illegal
        super();
        new Local();       // this is ok!
    }
}

Note, this is a subtlety that didn't exist before, because there was no way to declare a local class that required an outer instance before `super()` and then instantiate it after `super()`. Therefore, there was no reason to allow even the declaration such a class.

-------------

PR Comment: https://git.openjdk.org/jdk/pull/13656#issuecomment-1732194677


More information about the compiler-dev mailing list