Question re: JEP 492 specification

Chen Liang liangchenblue at gmail.com
Tue Dec 10 22:47:35 UTC 2024


Hi Archie,
I think this rule makes sense - Local1 can capture local variables in the
outer class initializer, in which case the error will be legitimate. The
extra otherwise does not fix this issue. Adding a special case to allow
no-capture local classes to be used in more nested static contexts seems
overkill and error-prone.

Chen

On Tue, Dec 10, 2024, 3:57 PM Archie Cobbs <archie.cobbs at gmail.com> wrote:

> The proposed JLS changes for JEP 492 are saying that this example should
> no longer compile*:
>
>     static {
>         class Local1 {
>             class Local2 {
>                 public static void m() {
>                     new Local1();  // error
>                 }
>             }
>         }
>     }
>
> but that seems wrong - that exampple has been allowed since JDK-8254321,
> and as it has nothing to do with flexible constructors, it should continue
> to be allowed.
>
> The JEP 492 spec says (new additions in bold):
>
>   If *C* is an inner local class, then:
>
>    -
>
>    If *C* 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.*
>
> Should there be an *Otherwise* at the beginning of the newly added
> sentence? (And same thing in the previous paragraph regarding anonymous
> classes)
>
> -Archie
>
> * See JDK-8345953 <https://bugs.openjdk.org/browse/JDK-8345953> JEP 492:
> instantiating local classes in a different static context should not be
> allowed
>
> --
> Archie L. Cobbs
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/amber-spec-experts/attachments/20241210/7d57825a/attachment.htm>


More information about the amber-spec-experts mailing list