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

Invalid compiler error with early reference in nested class

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P4 P4
    • 24
    • 24
    • tools
    • jdk-24+5-72-g5c612c230b0

    • 24
    • generic
    • generic

      The following example should compile, but does not:

      class C1 {
          String f1 = "f1";
          C1() {
              class C2 { // not early for C1
                  C2() {
                      class C3 { // early for C2
                          C3() {
                              f1.hashCode(); // compiles ok
                              class C4 { // not early for C3
                                  C4() {
                                      f1.hashCode(); // bogus error
                                  }
                              }
                          }
                      }
                      super();
                  }
              }
          }
      }

      $ javac -d classes --release 24 --enable-preview C1.java
      C1.java:11: error: no enclosing instance of type C1 is in scope
                                      f1.hashCode(); // bogus error
                                      ^
      Note: C1.java uses preview features of Java SE 24.

      Neither reference to "f1" is in the early initialization context of C1, so they should both be allowed.

            Unassigned Unassigned
            acobbs Archie Cobbs
            Votes:
            1 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: