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

Archie Cobbs acobbs at openjdk.org
Mon May 22 14:45:51 UTC 2023


On Mon, 22 May 2023 04:15:35 GMT, Chen Liang <liach at openjdk.org> wrote:

> what happens if you declare a local class or an anonymous class in the ctor prologue?

You can declare such a class in the prologue, but you can't instantiate it there:

    public static class LocalTest {
        public LocalTest() {
            class Foo {
                Foo() {
                    LocalTest.this.hashCode();
                }
            }
            new Foo();    // ILLEGAL
            super();
            new Foo();    // ALLOWED 
        }
    }       


Note that:
* The prologue includes the statements prior to the superclass constructor *and* the parameter expressions within the superclass constructor invocation
* The rules that apply to the prologue are the *same rules* that previously applied only to parameter expressions within the superclass constructor invocation

So if we "backport" your question to JDK 20, you get the current behavior:

import java.util.concurrent.atomic.*;
public class LocalTest extends AtomicReference<Object> {
    public class Inner {
    }
    public LocalTest(int x) {
        super(new Inner());     // ILLEGAL
    }
    public LocalTest(char x) {
        super(null);
        new Inner();            // ALLOWED
    }
}

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

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


More information about the compiler-dev mailing list