<p dir="ltr">Hi Archie,<br>
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.</p>
<p dir="ltr">Chen</p>
<br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Tue, Dec 10, 2024, 3:57 PM Archie Cobbs <<a href="mailto:archie.cobbs@gmail.com">archie.cobbs@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div dir="ltr"><div><div>The proposed JLS changes for JEP 492 are saying that this example should no longer compile*:<br></div></div><div><br></div><div> static {<br> class Local1 {<br> class Local2 {<br> public static void m() {<br> new Local1(); // error<br> }<br> }<br> }<br> }</div><div><br></div><div>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.<br></div></div><div dir="ltr"><br></div><div>The JEP 492 spec says (new additions in bold):</div><div><p> If <em>C</em> is an inner local class, then:</p>
<ul><li><p>If <em>C</em> occurs in a static context, then <em>i</em> has no
immediately enclosing instance. <strong>Let <em>S</em> be the nearest
<code>static</code> method declaration, <code>static</code> field
declaration, or static initializer that encloses the declaration of
<em>C</em>. If the nearest <code>static</code> method declaration,
<code>static</code> field declaration, or static initializer that
encloses the class instance creation expression is not <em>S</em>, then
a compile-time error occurs.</strong></p>
</li></ul></div><div>Should there be an <b>Otherwise</b> at the beginning of the newly added sentence? (And same thing in the previous paragraph regarding anonymous classes)<br></div><div><br></div><div>-Archie<br></div><div dir="ltr"><br></div><div dir="ltr">* See <a href="https://bugs.openjdk.org/browse/JDK-8345953" target="_blank" rel="noreferrer">JDK-8345953</a> JEP 492: instantiating local classes in a different static context should not be allowed</div><div dir="ltr"><span class="gmail_signature_prefix"><br></span></div><div dir="ltr"><span class="gmail_signature_prefix">-- </span><br><div dir="ltr" class="gmail_signature" data-smartmail="gmail_signature">Archie L. Cobbs<br></div></div>
</div>
</blockquote></div>