<div dir="auto">I was extremely extremely sad when they said no JEP 301. 🥺<div dir="auto"><br></div><div dir="auto">Enums would be literally perfect if we had that feature. And Java enums are so much better than literally any other non-JVM languages enums, so part of me also wishes we could widen the gap further lol.</div><div dir="auto"><br></div><div dir="auto">And to clarify, when I say the word enum, I am referring to an enumerated set of VALUES, not TYPES. Rust and friends like to use enums to do types, which is not the same thing. That's more like our sealed types.</div><div dir="auto"><br></div><div dir="auto">But as far as enum of values, we are uncontested, save for other JVM languages.</div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Wed, Feb 21, 2024, 9:27 AM Tagir Valeev <<a href="mailto:amaembo@gmail.com">amaembo@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">There's a withdrawn JEP 301 which tried to address this issue, among other things (see "Sharper typing of enum constants" section):<br><div><a href="https://openjdk.org/jeps/301" target="_blank" rel="noreferrer">https://openjdk.org/jeps/301</a><br></div><div><br></div><div>Unfortunately, this was never implemented due to issues in the part that suggested making enums generic. I'm not aware of issues in "sharper typing" alone (without the "generic" feature). Probably it could be done separately, but likely nobody wants to invest resources in this.</div><div><br></div><div>With best regards,</div><div>Tagir Valeev.</div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Wed, Feb 21, 2024 at 1:14 PM Pavel Rappo <<a href="mailto:pavel.rappo@oracle.com" target="_blank" rel="noreferrer">pavel.rappo@oracle.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">From <a href="https://mail.openjdk.org/mailman/listinfo/discuss" rel="noreferrer noreferrer" target="_blank">https://mail.openjdk.org/mailman/listinfo/discuss</a>, this mailing list is not suitable for questions like this. I'm not sure which mailing list is *the most* suitable, but the following one is surely more suitable: <a href="https://mail.openjdk.org/pipermail/amber-dev/" rel="noreferrer noreferrer" target="_blank">https://mail.openjdk.org/pipermail/amber-dev/</a> (CC'ed).<br>
<br>
I believe, the reasons why the first snippet of yours works, but the second doesn't, is covered here:<br>
<br>
 * <a href="https://docs.oracle.com/javase/specs/jls/se21/html/jls-8.html#jls-8.9.1" rel="noreferrer noreferrer" target="_blank">https://docs.oracle.com/javase/specs/jls/se21/html/jls-8.html#jls-8.9.1</a> (like you said)<br>
 * <a href="https://docs.oracle.com/javase/specs/jls/se21/html/jls-14.html#jls-14.4.1" rel="noreferrer noreferrer" target="_blank">https://docs.oracle.com/javase/specs/jls/se21/html/jls-14.html#jls-14.4.1</a><br>
<br>
I would be surprised if that could change to allow your second snippet. That said, if that functionality is truly required, you can provide it yourself.<br>
<br>
The design of enums allows you to declare a method on an enum class and then override it in a particular constant. Alternatively, you can switch on an enum constant to provide constant-specific behaviour "externally".<br>
<br>
-Pavel<br>
<br>
> On 21 Feb 2024, at 10:08, Red IO <<a href="mailto:redio.development@gmail.com" target="_blank" rel="noreferrer">redio.development@gmail.com</a>> wrote:<br>
> <br>
> I recently tinkered a bit with java enums. While I was researching edge features enums support I came across a stackoverflow post referencing the JLS 8.9 saying that the optional body of an enum constant is an anonymous class declaration. <br>
> As for some time now methods declared in an anonymous class assigned to a var variable are accessible I tried to do the same for the enum constant. <br>
> <br>
> var anonymous = new Object() {<br>
> public void test() {} <br>
> }<br>
> anonymous.test(); //works<br>
> <br>
> <br>
> enum Test {<br>
> A {<br>
> public void test() {} <br>
> } <br>
> }<br>
> var enumConstant = Test.A;<br>
> enumConstant.test(); // doesn't work<br>
> <br>
> I have a clue to why this doesn't work. It's likely that the type of the constant A is the type of the enum and not of the anonymous class. <br>
> <br>
> There are some usecases where exclusive methods on enum constants might be useful. So it would be nice if this would work. <br>
> <br>
> Great regards <br>
> RedIODev <br>
> <br>
> <br>
> <br>
> <br>
<br>
</blockquote></div>
</blockquote></div>