The compiler is generating a bogus error when an unqualified field "x" is assigned in an early construction context of class C when:
(a) "x" is declared as a private instance field in a superclass S of C *and*
(b) S is also an outer class of C.
In this case the error analysis is mistaking "x" as a reference to "this.x" when it is really a reference to "S.this.x" (because "this.x" is inaccessible, being private).
Reproducer:
$ java -version
openjdk version "24" 2025-03-18
OpenJDK Runtime Environment (build 24+36-3646)
OpenJDK 64-Bit Server VM (build 24+36-3646, mixed mode, sharing)
$ cat Outer.java
class Outer {
private int i;
class Sub extends Outer {
Sub() {
i = 42;
super();
}
}
}
$ javac --enable-preview --source 24 Outer.java
Outer.java:5: error: cannot reference i before supertype constructor has been called
i = 42;
^
Note: Outer.java uses preview features of Java SE 24.
Note: Recompile with -Xlint:preview for details.
1 error
amber-dev thread: https://mail.openjdk.org/pipermail/amber-dev/2025-February/009224.html
(a) "x" is declared as a private instance field in a superclass S of C *and*
(b) S is also an outer class of C.
In this case the error analysis is mistaking "x" as a reference to "this.x" when it is really a reference to "S.this.x" (because "this.x" is inaccessible, being private).
Reproducer:
$ java -version
openjdk version "24" 2025-03-18
OpenJDK Runtime Environment (build 24+36-3646)
OpenJDK 64-Bit Server VM (build 24+36-3646, mixed mode, sharing)
$ cat Outer.java
class Outer {
private int i;
class Sub extends Outer {
Sub() {
i = 42;
super();
}
}
}
$ javac --enable-preview --source 24 Outer.java
Outer.java:5: error: cannot reference i before supertype constructor has been called
i = 42;
^
Note: Outer.java uses preview features of Java SE 24.
Note: Recompile with -Xlint:preview for details.
1 error
amber-dev thread: https://mail.openjdk.org/pipermail/amber-dev/2025-February/009224.html
- links to
-
Review(master) openjdk/jdk/23545