Enums
This data structure allows defining a set of values, and assigning them to an object.
Enums as Classes
Java enums work similar to any class, which means they can implement interfaces, and contain custom methods.
Enums as Data Structures
As they are similar to other classes they can be used to create data structures, for example each value may contain a String:
As enum constructors should be private, there is no way to create a value which is not contained in the enum.
Switchs
Enums are easy to use in switchs:
Finding Values
Enums contain all the fields defined on them, and these can be acquired easily:
Working with the ordinal value is not recommended, as this will change of the fields are reordered:
It is better using the string value:
More Information
Last updated
Was this helpful?