Java records used in enums

Alberto Otero Rodríguez albest512 at hotmail.com
Fri May 21 15:28:43 UTC 2021


The problem in my case was that we have an enum ErrorEnum with several fields.

One of the fields was "description" which have a getter and a setter.

The problem appeared when testing with JUnit. Basically the logic of the application was working correctly but the descriptions of the errors of different tests were overriden with the description of the errors of another tests.

The problem was solved by making the fields of the enums final and removing all the setter methods (after trying a lot of things and refactoring the application almost entirely to remove all the static methods and static members we had in the application).

Regards,

Alberto.
________________________________
De: core-libs-dev <core-libs-dev-retn at openjdk.java.net> en nombre de liangchenblue at gmail.com <liangchenblue at gmail.com>
Enviado: viernes, 21 de mayo de 2021 17:15
Para: core-libs-dev at openjdk.java.net <core-libs-dev at openjdk.java.net>
Asunto: Fwd: Java records used in enums

---------- Forwarded message ---------
From: - <liangchenblue at gmail.com>
Date: Fri, May 21, 2021 at 10:14 AM
Subject: Re: Java records used in enums
To: Alberto Otero Rodríguez <albest512 at hotmail.com>


I fail to understand what you want. How does making enums records fix the
multithreading issue?

If you wish to have enums declared like records code-wise, I see it as a
possibility, but I don't see how it affects any enum behavior in any way.

If you want a combination of record and enum, or a record with enumerated
possible instances, it may be possible to add an alternative syntax for
such enum declaration; but restricting all enums to be such enum records
offers no benefit that I can see, especially given records themselves have
severe restrictions (must have an accessible canonical ctor available; that
ctor cannot throw checked exceptions; field/getter method name limits, so a
"enum record" cannot declare fields like "values" "name"); also records are
value-based while enums are identity based, which is another problem.

In short, I believe regular enum suffices and a "record enum" offers little
benefit while it cannot fix your issue at all.

On Fri, May 21, 2021 at 10:00 AM Alberto Otero Rodríguez <
albest512 at hotmail.com> wrote:

> It seems a non-existent problem until you face it, which is what happened
> me today.
>
> I think at least (supposing enums can't be records) all the fields in
> enums should be made final by default.
>
> Regards,
>
> Alberto.
> ________________________________
> De: Kasper Nielsen <kasperni at gmail.com>
> Enviado: viernes, 21 de mayo de 2021 16:28
> Para: Alberto Otero Rodríguez <albest512 at hotmail.com>
> Cc: core-libs-dev at openjdk.java.net <core-libs-dev at openjdk.java.net>
> Asunto: Re: Java records used in enums
>
> On Fri, 21 May 2021 at 14:51, Alberto Otero Rodríguez <
> albest512 at hotmail.com<mailto:albest512 at hotmail.com>> wrote:
> Hi,
>
> I think enums in Java should be immutable. When you let the programmer
> change values in an enum instance, an unexpected behaviour can happen when
> using multiple threads as enum instances are static (singleton).
>
> So, I was wondering why not make enums instances be defined as records
> instead of normal classes.
>
> Lots of reasons, for example:
> * It would be a breaking change for an almost non-existent problem.
> * All internal state of enums would now be public.
> * Enum's extend java.lang.Enum, records extend java.lang.Record.
>   java.lang.Enum cannot extend java.lang.Record because it has instance
> fields.
>
> /Kasper
>


More information about the core-libs-dev mailing list