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

JEP 492: extending local class in a different static context should not be allowed

XMLWordPrintable

    • b03

        According to JEP 492 spec https://cr.openjdk.org/~gbierman/jep492/jep492-20241101/specs/flexible-constructor-bodies-jls.html,

        If S is a local class whose declaration occurs in a static context, let N be the nearest static method declaration, static field declaration, or static initializer that encloses the declaration of S. If the nearest static method declaration, static field declaration, or static initializer that encloses the class instance creation expression is not N, then a compile-time error occurs.

        So this code should fail, but it compiles:

        class O {
            static void foo(int x) { // static context, so no enclosing instance
                class C { /* capture x */ }
                class U {
                    static void test() {
                        class L extends C {
                            L() {
                                super();// compilation succeeds
                            }
                        }
                    }
                }
            }
        }

        java --version
        java 24-ea 2025-03-18
        Java(TM) SE Runtime Environment (build 24-ea+26-3339)
        Java HotSpot(TM) 64-Bit Server VM (build 24-ea+26-3339, mixed mode, sharing)

              mcimadamore Maurizio Cimadamore
              eananeva Ella Ananeva
              Votes:
              1 Vote for this issue
              Watchers:
              5 Start watching this issue

                Created:
                Updated:
                Resolved: