Learn Java enum with Examples

The enum type in Java

The Java enum type enables creating the variable with the pre-defined set of constants. This is required in certain situations where you need to restrict the value of a variable to be among the pre-defined values.

For example, the Day enum can be defined with pre-defined values: MONDAY, TUESDAY …. SUNDAY. Similarly, the Month enum can be defined with month names JAN, FEB, MAR….DEC. The compass direction enum can be defined with NORTH, EAST, WEST, and SOUTH.

These are natural enums types. You may use enum types for any scenario where you know all the possible values at compile time. For example, your program has a menu with certain options. A user may select only among the given options in the menu.

Also, you noticed in above constants of enums; the values are kept uppercase. This is because the enum type fields are constants.

In the following section, I will show you the examples of defining and using enums in Java programs.

How to define enum Java type?

The enum is defined by using the enum keyword followed by name of your choice, for example:

An example of defining and using enum in Java program

In this example, an enum with the name of Months is defined. The enum constants are the short names of the Months like JAN, FEB, MAR and so on. After defining the enum, the values are displayed by using a for loop as shown below:

Java enum

See online demo and code

The Java enum code:

 

An example of using enum with switch statement

In this example, an enum of Capitals is defined. I used only four country names for the demo. This is followed by a function with the switch statement.

Inside the function, the value of the enum is passed to the switch statement. For all possibilities, different cases are created that returns the capital name of the given country as calling the function. Have a look:

enum switch

See online demo and code

The enum Java code with switch statement:

 

You can see, this line of code calls the function with a value of enum constant:

System.out.println(theCapitalName(Capitals.USA));

Just change the name from the USA to other countries from the Capitals enum constants and it will  display the capital name.

Note: if you try using some other name that does not exist in the enum constants, an error will generate.

A few important points about enums in Java

  • The enum was introduced in JDK 1.5.
  • The enum is a special data type that enables defining the variable with the pre-defined set of constants.
  • The enums extend the java.lang.Enum. As it extends a class, so you cannot extend an enum (because this is not supported in Java).
  • It improves type safety as it can be ensured to limit for a particular set of constants.
  • The enum can be used with switch statement easily.
  • The enum constants are defined with uppercase letters.
  • The enum is static and final implicitly. Once it is created, it cannot be changed.
  • The Java enum may contain fields, constructors, and methods.