What are enums?
Enums are classes where all instances are known to the compiler. They are used for creating types that can only have few possible values.
Enums can be created similar to classes but use the enum keyword instead of class.
In the body, there is a list of instances of the enum called enum constants which are separated by ,.
No instances of the enum can be created outside of enum constants.
All enums implicitly extend java.lang.Enum and cannot have any named subclasses.
Accessing, evaluating, and comparing enums
The values of an enum can be used as constants.
In order to check whether two instances of an enum are the same, the == operator can be used.
It is also possible to use switch for performing actions depending on the value of the enum.
With switch expressions, the compiler can check whether all values of the enum are handled. If any possible value is missing in a switch expression, there will be a compiler error. This is referred to as Exhaustiveness and can also be achieved with regular classes through JEP 409: Sealed Classes and pattern matching.
Adding members to enums
Just like classes, enums can have constructors, methods and fields.
In order to add these, it is necessary to add a ; after the list of enum constants.
Arguments to the constructor are passed in parenthesis after the declaration of the enum constant.
Special methods
All enums have a few methods that are added implicitly.
For example, the method name() is present in all enum instances and can be used to get the name of the enum constant.
Similarly, a method named ordinal() returns the position of the enum constant in the declaration.
Aside from instance methods, there are also static methods added to all enums.
The method values() returns an array containing all instances of the enum and the method valueOf(String) can be used to get a specific instance by its name.
Furthermore, enums implement the interface Comparable.
By default, enums are ordered according to their ordinal number
i.e. in the order of occurrence of the enum constant.
This allows for comparing instances of enums as well as sorting or searching.
Using enums as singletons
Since enums can only have a specific number of instances, it is possible to create a singleton by creating an enum with only a single enum constant.
Abstract methods in enums
Even though enums cannot be extended, they can still have abstract methods. In that case, an implementation must be present in each enum constant.
Precautions
Care should be taken when using enums where the number (or names) of instances is subject to change.
Whenever enum constants are changed, other code expecting the old version of the enum might not work as expected.
This may manifest in compilation errors (e.g. when referencing a removed enum constant),
runtime errors (e.g. if there is a default case even though the new enum constant should be handled separately)
or other inconsistencies (e.g. if the value of the enum was saved to a file which is then read and expecting that value to still exist).
When changing enum constants, it is recommended to review all code using the enum. This is especially important in cases where the enum is also used by other people's code.
Furthermore, it might be worth considering to use other options in case of many instances since listing a lot of instances at a single location in code can be inflexible. For example, it may be better to use a configuration file for listing all instances and reading these configuration files in the program in cases like this.
Conclusion
Enums provide a simple and safe way of representing a fixed set of constants while keeping most of the flexibilities of classes. They are a special type of class that can be used to write code that is elegant, readable, maintainable, and works well with other modern Java features like switch expressions. Another special class is the Record class added in Java 16. You can visit our Record section to learn more.
To learn more about enums, visit the java.lang.Enum javadoc.