Creating and Using Enums in Java

Waqas Anwar
13 March 2008
16327 Views

In J2SE 5.0 Java introduced typesafe enumerations called “Enum” or simply “enums”. They are used to create multiple constants with a single given name.

Prior to Java 5.0, you had to use static final int constants to create a list of enumerated variables as following example.

public static final int GENDER_MALE = 0;
public static final in GENDER_FEMALE = 1;

Although the above code solves the problem but this pattern of creating enumerated type has some problems such as the above variables are not typesafe since both variables GENDER_MALE and GENDER_FEMALE are just int variables so you can pass in any other int value where one of the above constant gender value is required.

Secondly if you print any of the above variables all you will get is a number such as 0 or 1, since they are just integers. You cannot get information about variables for example what type of gender this number represents.

So java 5.0 enums solve both above problems and in addition they provide many more useful features than their counterparts in other languages. In their simplest form, enums in java have same syntax as C, C++ or C# languages:

 

enum MovieTypes
{
   ACTION,
   HORROR,
   COMEDY
}

or you can also use single line syntax

enum WeekDays { MON, TUE, WED, THU, FRI, SAT, SUN }

 

Every time you create enum in java you are creating a full fledged enum type or class and by default they provide basic implementation of all Object class methods. In addition to getting a complete enum type in your code, they are also Comparable and Serializable.

After your enum is created you can declare variables of enum type to store one of the enum predefined variable.

MovieTypes m1 = MovieTypes.HORROR;
MovieTypes m2 = MovieTypes.ACTION;


If you want to print enum variable you can print directly or you can call methods available to all enums in java.

System.out.println(m1);   
System.out.println(m1.name());
System.out.println(m1.toString());


or you can print any of the variable directly with the enum name

System.out.println(MovieTypes.ACTION);
System.out.println(MovieTypes.HORROR);


If you want to iterate all the enum variables then you can use static method values() which is available in all enums in java and return an array of enum type

MovieTypes[] movies = MovieTypes.values();

for(int i=0 ; i<movies.length ; i++)
{
   System.out.println(movies[i]);
}

you can also use new java foreach loop to iterate enum variables

for(MovieTypes m: MovieTypes.values())
{
   System.out.println(m);
}

As I said above enums in java are Comparable so you can compare two enum type variables as follows:

MovieTypes m1 = MovieTypes.HORROR;
MovieTypes m2 = MovieTypes.HORROR;
MovieTypes m3 = MovieTypes.ACTION;

System.out.println(m1.equals(m2));
System.out.println(m1.equals(m3));

Enum type variable can also be used with switch statement to test its value with multiple cases

MovieTypes m = MovieTypes.ACTION;

switch(m)
{
   case COMEDY:
      System.out.println(m); break;
   case HORROR:
      System.out.println(m); break;
   case ACTION:
       System.out.println(m); break;
}

You should used enums in java every time you need a fixed set of constants or you want to restrict a variable to hold predefined set of constant values defined in enum. You should also use them where you have some predefined set of values you want to user to input in your program such as menu choices.