Enum in Java With Examples

Enum was introduced in Java 1.5 as a new type whose fields consists of a fixed set of constants. For example, we can create directions as Java Enum with fixed fields as EAST, WEST, NORTH, and SOUTH.

Java Enum

In this tutorial, we will learn know how to create an Enum. We will also look into the benefits of using enums in java and features of enum types. We will also learn using Java Enum valueOf, enum values, EnumSet and EnumMap with examples.

Java Enum Example

Java enum keyword is used to create an enum type. Let’s have a look at the java enum example program.

In above example, ThreadStates is the enum with fixed constants fields START, RUNNING, WAITING and DEAD.

Java Enum vs Constants

Now let’s see how java enum is better than normal constants fields in Java classes.

Let’s create a similar constants class in java.

Now let’s see how both enum and constants are used in a java program:

If we look at the above example, we have two risks with using constants that are solved by the enum.

  1. We can pass any int constant to the simpleConstantsExample method but we can pass only fixed values to simpleEnumExample, so it provides type safety.
  2. We can change the int constants value in ThreadStatesConstant class but the above program will not throw any exception. Our program might not work as expected but if we change the enum constants, we will get compile time error that removes any possibility of runtime issues.

Java Enum Methods

Now let’s see more features of java enum with an example.

Java Enum Important Points

Below are some of the important points for Enums in Java.

  1. All java enum implicitly extends java.lang.Enum class that extends Object class and implements Serializable and Comparable interfaces. So we can’t extend any class in enum.
  2. Since enum is a keyword, we can’t end package name with it, for example com.journaldev.enum is not a valid package name.
  3. Enum can implement interfaces. As in above enum example, it’s implementing Closeable interface.
  4. Enum constructors are always private.
  5. We can’t create instance of enum using new operator.
  6. We can declare abstract methods in java enum, then all the enum fields must implement the abstract method. In above example getDetail() is the abstract method and all the enum fields have implemented it.
  7. We can define a method in enum and enum fields can override them too. For example, toString() method is defined in enum and enum field START has overridden it.
  8. Java enum fields has namespace, we can use enum field only with class name like ThreadStates.START
  9. Enums can be used in switch statement, we will see it in action in the later part of this tutorial.
  10. We can extend existing enum without breaking any existing functionality. For example, we can add a new field NEW in ThreadStates enum without impacting any existing functionality.
  11. Since enum fields are constants, java best practice is to write them in block letters and underscore for spaces. For example EAST, WEST, EAST_DIRECTION etc.
  12. Enum constants are implicitly static and final
  13. Enum constants are final but it’s variable can still be changed. For example, we can use setPriority() method to change the priority of enum constants. We will see it in usage in below example.
  14. Since enum constants are final, we can safely compare them using “==” and equals() methods. Both will have the same result.

Java EnumSet, EnumMap, valueOf()

Now we know most of the features of Enum, let’s have a look at Java Enum example program. Then we will learn some more features of an enum.

Before explaining other important features of enum, let’s see the output of the above program.

Important Points

  1. The usingEnumMethods() methods shows how to create an enum object and how we can use its methods. It’s also showing use of setPriority(int i) method to change the variable of enum.
  2. usingEnumValueOf() shows the usage of java.util.Enum valueOf(enumType, name) through which we can create an enum object from String. It throws IllegalArgumentException if the specified enum type has no constant with the specified name, or the specified class object does not represent an enum type. It also throws NullPointerException if any of the arguments are null.
  3. usingEnumValues() method shows the usage of values() method that returns an array containing all of the values of the enum in the order they are declared. Note that this method is automatically generated by java compiler for every enum. You won’t find values() implementation in java.util.Enum class.
  4. The usingEnumInSwitch() method shows how to use enum constants in switch case.
  5. usingEnumMap() method shows use of java.util.EnumMap, which is introduced in Java 1.5 Collections Framework. EnumMap is Map implementation for use with enum type keys. All of the keys in an enum map must come from a single enum type that is specified, explicitly or implicitly, when the map is created. We can’t use null as key for EnumMap and EnumMap is not synchronized.
  6. usingEnumSet() method shows use of java.util.EnumSet, which is Set implementation for use with enum types. All of the elements in an enum set must come from a single enum type that is specified, explicitly or implicitly, when the set is created. EnumSet is not synchronized and null elements are not allowed. It also provides some useful methods like copyOf(Collection<E> c), of(E first, E... rest) and complementOf(EnumSet<E> s).

Reference: Oracle Doc

By admin

Leave a Reply