Learn Java enum with Examples

Informative infographic showcasing the key features of Java Enums. Explore constants, methods, and practical use cases.

The enum type in Java

  • 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.
public enum Months { JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC }
  • These are natural enum 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 the above constants of enums that the values are kept in uppercase. This is because the enum type fields are constants.

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

How to define Java enum type?

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

public enum Months {

JAN, FEB, MAR, APR, MAY,

JUN, JUL, AUG, SEP, OCT, NOV, DEC

}

An example of defining and using an enum in a 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:

The Java enum code:

public class enum_demo {

        public enum Months { JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC } 


        public static void main(String[] args) { 

        System.out.println("The enum values:");            

        System.out.println("\n");

        for (Months month_name : Months.values()) 

        System.out.println(month_name); 
     }

}

Output:

The enum values:
JAN
FEB
MAR
APR
MAY
JUN
JUL
AUG
SEP
OCT
NOV
DEC

An example of using an enum with the 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 return the capital name of the given country as calling the function.

Java enum code with a switch statement:

public class enum_demo

{
  enum Capitals
  {
    USA, UK, GERMANY, FRANCE
  }

  public static void main(String[] args)
  {
     System.out.println("Displaying the enum with switch statement!"); 
     System.out.println("\n");
     System.out.println(theCapitalName(Capitals.USA));
 }

  public static String theCapitalName(Capitals country_name)

  {
    switch (country_name)
    {
      case USA:    
            return "The Capital of USA is: Washington, D.C.";

      case UK:  
           return "The Capital of UK is: London";

      case GERMANY: 
            return "The Capital of GERMANY is: Berlin";

      case FRANCE:   
            return "The Capital of FRANCE is: Paris";

      default:     
            return "Select a valid country!";

    }

  }
}

Output:

Displaying the enum with switch statement!

The Capital of USA is: Washington, D.C

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 be generated.

Summarizing 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, 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.

 

Author - Atiq Zia

Atiq is the writer at jquery-az.com, an online tutorial website started in 2014. With a passion for coding and solutions, I navigate through various languages and frameworks. Follow along as we unravel the mysteries of coding together!