Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8349754

Invalid "early reference" error when class extends an outer class

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Unresolved
    • Icon: P4 P4
    • 25
    • 24.0.2
    • tools
    • None
    • 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)

    • 24
    • generic
    • generic

      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

            acobbs Archie Cobbs
            acobbs Archie Cobbs
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated: