Java ENUM with multiple values and record class

What is an enum class?

An enum class is like a list of predefined options that you can choose from. It helps make your code clearer and safer by limiting the possible choices to a specific set of values. It’s useful when you have a variable that can only have a certain number of options, and you want to make sure it’s always set to one of those options.

How to build one?

a classic enum class with getters

Java

an enum class with record

Java

To call them, thy both work almost the same way:

Java

In conclusion, if you are using a big list of enums (who would do that, for heaven’s sake?) using the record class is better. If you only have a few constants, the classic way is much cleaner.


Leave a Reply

Your email address will not be published. Required fields are marked *