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

15.9.2: Assertion regarding local class creation and static contexts should refer to direct superclass of the anonymous class, not the anonymous class itself

XMLWordPrintable

    • b03

        This regards the PREVIEW JEP 492:

        Consider this case:

        ```
        class O {
            static void foo(int x) { // static context, so no enclosing instance
                class X { /* capture x */ }
                class U {
                    static void test() { new X(){...}; } // just as bad as saying `new X()`
                }
            }
        }
        ```

        The anonymous class instance creation expression should be forbidden (as it would be if it was just `new X()`). The rule given in the draft spec says:

        - If C is an anonymous class, then:

           - If the class instance creation expression occurs in a static context, then i has no immediately enclosing instance. Let S be the nearest static method declaration, static field declaration, or static initializer that encloses the declaration of C. If the nearest static method declaration, static field declaration, or static initializer that encloses the class instance creation expression is not S, then a compile-time error occurs.

        The text "... that encloses the declaration of C" is incorrect and should read "... that encloses the declaration of **the direct superclass or direct superinterface** of C"



              gbierman Gavin Bierman
              gbierman Gavin Bierman
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

                Created:
                Updated:
                Resolved: