Apart from Class and Interface, Enumeration type or Enum is another popular type in Java programming language. Enum allows you to represent fixed number of things in a type-safe manner e.g. days of week, days of month, planets in solar system, buttons on remote control, keys on keyboard and suits on playing cards. Before Enum was introduced, prior to Java 1.5,
integer and
string constants are used to represent fixed number of things, known as
enum int pattern and
enum string pattern. Though they serve the purpose, they had some major drawbacks, one of them was
type-safety i.e. you cannot restrict them to represent fixed number of values e.g. an integer constant, which is representing days of week can have value as 100, which is incorrect, given we have only 7 days in a week. These limitation are addressed by providing
enum type, which guarantees that only correct and fixed number of values can be assigned to them. Good thing about Enum in Java is that they are feature rich, bad thing is that they are still under-used and many developers doesn't even know about Enumeration type in detail. Though there are lot of blog post about different features of Enum in Java, and there usage, there is hardly any page, which provides a good look on key points about Enum. It doesn't mean to be comprehensive but at-least provides those lesser known details. I had previously shared some features in form of question answer on my article
15 Java Enum Interview Questions and in this post we will learn more about Enum in Java, it's more like a summary and notes than a tutorial, which is meant to quickly realize power of Enumeration type in Java.