Anonymous classes of enum constants and var

Red IO redio.development at gmail.com
Wed Feb 21 18:02:04 UTC 2024


This is nice and well. But it isn't the same thing. Sure this works well if
every varient does exactly the same thing. But say you have a enum having
varients that share a feature but have their own unique properties. Like a
rule set to initialize something and an associated constants or operation
that only makes sense for 1 varient. Currently you need to define both
public constants and public methods in the enum associating it with the
whole enum instead of the varient that works with it. In methods that gets
even worse as you need to provide a default operation that either does
nothing or throws for incompatible varients. By simply typing the enum
constant to the anonymous class this would become possible.

Great regards
RedIODev

On Wed, Feb 21, 2024, 14:49 David Alayachew <davidalayachew at gmail.com>
wrote:

> Idk which is the right group, but I choose amber.
>
> I just want to add a couple of clarifications on the various different
> forms (I spend a LOT of my time woth enums).
>
>
>    - You can use an abstract method like so. It has the benefit of
>    forcing every enum value to provide their own implementation of the
>    abstract method. Think of it like a switch with no default clause.
>
>
> enum Blah
> {
>
>    VALUE1
>    {
>
>       void method()
>       {
>
>          println("blah");
>
>       }
>
>    },
>    ;
>
>    abstract void method();
>
> }
>
>
>    - Alternatively, you can provide an implementation of the method. This
>    is more like a switch with a default clause.
>
>
> enum Blah
> {
>
>    VALUE1
>    {
>
>       void method()
>       {
>
>          println("blah");
>
>       }
>
>    },
>    ;
>
>    void method()
>    {
>
>       println("default");
>
>    }
>
> }
>
>
>    - And of course, you could model this externally via a switch case.
>    Both of the above bullets are modelable as switch expressions (as explained
>    already). The benefit is that they can keep the enum simple, but the cost
>    is that you must explicitly know the implementation you want instead of
>    having one provided for you.
>
>
>
>
> On Wed, Feb 21, 2024, 7:14 AM Pavel Rappo <pavel.rappo at oracle.com> wrote:
>
>> From https://mail.openjdk.org/mailman/listinfo/discuss, 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:
>> https://mail.openjdk.org/pipermail/amber-dev/ (CC'ed).
>>
>> I believe, the reasons why the first snippet of yours works, but the
>> second doesn't, is covered here:
>>
>>   *
>> https://docs.oracle.com/javase/specs/jls/se21/html/jls-8.html#jls-8.9.1
>> (like you said)
>>   *
>> https://docs.oracle.com/javase/specs/jls/se21/html/jls-14.html#jls-14.4.1
>>
>> 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.
>>
>> 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".
>>
>> -Pavel
>>
>> > On 21 Feb 2024, at 10:08, Red IO <redio.development at gmail.com> wrote:
>> >
>> > 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.
>> > 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.
>> >
>> > var anonymous = new Object() {
>> > public void test() {}
>> > }
>> > anonymous.test(); //works
>> >
>> >
>> > enum Test {
>> > A {
>> > public void test() {}
>> > }
>> > }
>> > var enumConstant = Test.A;
>> > enumConstant.test(); // doesn't work
>> >
>> > 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.
>> >
>> > There are some usecases where exclusive methods on enum constants might
>> be useful. So it would be nice if this would work.
>> >
>> > Great regards
>> > RedIODev
>> >
>> >
>> >
>> >
>>
>>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/amber-dev/attachments/20240221/fafcce31/attachment.htm>


More information about the amber-dev mailing list