Java enum tutorial

Java enum, also called Java enumeration type, is a type whose fields consist of a fixed set of constants. The very purpose of enum is to enforce compile time type safety. enum keyword is reserved keyword in Java.

We should use enum when we know all possible values of a variable at compile time or design time, though we can add more values in future as and when we identify them. In this java enum tutorial, we will learn what enums are and what problems they solve?

Table of Contents

1. What is enum in Java
2. enum constructors
3. enum methods
4. enum inheritance
5. Compare enums
6. EnumMap and EnumSet
7. Summary

1. enum in Java

Enumerations (in general) are generally a set of related constants. They have been in other programming languages like C++ from beginning. After JDK 1.4, Java designers decided to support it in Java also, and it was officially released in JDK 1.5 release.

Enumeration in Java is supported by keyword enum. enums are a special type of class that always extends java.lang.Enum.

1.1. enum is reserved keyword

enum in Java is reserved keyword. It means you cannot define a variable of name enum. e.g. It will result in compile time error "invalid VariableDeclaratorId".

enum is reserved keyword
enum is reserved keyword

1.2. Java enum declaration

A simple example to create enum. As we know, generally we deal with four directions in daily life. Their names, angles and other properties are fixed. So, in programs, we can create enum for them. Syntax to create an enum is as below:

public enum Direction 
{
   EAST, WEST, NORTH, SOUTH;
}

Logically, each enum is an instance of enum type itself. So given enum can be seen as below declaration. JVM internally adds ordinal and value methods to this class which we can call while working with enum.

final class Direction extends Enum<Direction> 
{
    public final static Direction EAST = new Direction();
    public final static Direction WEST = new Direction();
    public final static Direction NORTH = new Direction();
    public final static Direction SOUTH = new Direction();
}

1.3. Java enum example

We can use the enum just like we use final static class fields.

public class EnumExample 
{
    public static void main(String[] args) 
    {
        Direction north = Direction.NORTH;
        
        System.out.println(north);        //Prints NORTH
    }
}

1.4. enum ordinal()

The ordinal() method returns the order of an enum instance. It represents the sequence in the enum declaration, where the initial constant is assigned an ordinal of '0'. It is very much like array indexes.

It is designed for use by sophisticated enum-based data structures, such as EnumSet and EnumMap.

Direction.EAST.ordinal();     //0

Direction.NORTH.ordinal();    //2

1.5. enum values() and valueOf()

The enum values() method returns all the enum values in an enum array.

Direction[] directions = Direction.values();

for (Direction d : directions) {
    System.out.println(d);
}

//Output:

EAST
WEST
NORTH
SOUTH

The enum valueOf() method helps to convert string to enum instance.

Direction east = Direction.valueOf("EAST");
        
System.out.println(east);

//Output:

EAST

1.6. enum naming convention

By convention, enums are constants. In Java, constants are defined in all UPPER_CASE letters. This follows are enums also.

  • enum name should be in title case (same as class names).
  • enum fields should be in all UPPER CASE (same as static final constants).

2. enum constructors

By default, enums don’t require constructor definitions and their default values are always the string used in the declaration. Though, you can give define your own constructors to initialize the state of enum types.

For example, we can add angle attribute to direction. All directions have some angle. So let’s add them.

public enum Direction 
{
    // enum fields
    EAST(0), WEST(180), NORTH(90), SOUTH(270);

    // constructor
    private Direction(final int angle) {
        this.angle = angle;
    }

    // internal state
    private int angle;

    public int getAngle() {
        return angle;
    }
}

If we want to access angle for any direction, we can make a simple method call in enum field reference.

Direction north = Direction.NORTH;
        
System.out.println( north );                      //NORTH

System.out.println( north.getAngle() );           //90

System.out.println( Direction.NORTH.getAngle() ); //90

3. enum methods

Remember that enum is basically a special class type, and can have methods and fields just like any other class. You can add methods which are abstract as well as concrete methods as well. Both methods are allowed in enum.

3.1. concrete methods in enum

Adding a concrete method in enum is similar to add same method in any other class. You can use any access specifier e.g. public, private or protected. You can return values from enum methods or simply use them to perform internal logic.

public enum Direction 
{
    // enum fields
    EAST, WEST, NORTH, SOUTH;
    
    protected String printDirection() 
    {
        String message = "You are moving in " + this + " direction";
        System.out.println( message );
        return message;
    }
}

You can call printDirection() method as simple method calls on enum instance.

Direction.NORTH.printDirection(); //You are moving in NORTH direction
        
Direction.EAST.printDirection();  //You are moving in EAST direction

3.2. abstract methods in enum

We can add abstract method in enums. In this case, we must implement the abstract method at each enum field, individually.

public enum Direction 
{
    // enum fields
    EAST {
        @Override
        public String printDirection() {
            String message = "You are moving in east. You will face sun in morning time.";
            return message;
        }
    },
    WEST {
        @Override
        public String printDirection() {
            String message = "You are moving in west. You will face sun in evening time.";
            return message;
        }
    },
    NORTH {
        @Override
        public String printDirection() {
            String message = "You are moving in north. You will face head in daytime.";
            return message;
        }
    },
    SOUTH {
        @Override
        public String printDirection() {
            String message = "You are moving in south. Sea ahead.";
            return message;
        }
    };

    public abstract String printDirection();
}

Re-run above example.

Direction.NORTH.printDirection(); //You are moving in north. You will face head in daytime.
        
Direction.EAST.printDirection();  //You are moving in east. You will face sun in morning time.

You can enforce a contract for all enums to be created in this way. It can serve as template for enum creation.

For example, If we want that each enum type of Direction should be able to print the direction name with a custom message when needed. This can be done by defining a abstract method inside Direction, which each enum has to override. In future, in any more directions are added (really?), then we must add a custom message as well.

4. enum inheritance

As mentioned earlier, enums extends Enum class. java.lang.Enum is an abstract class. This is the common base class of all Java enumeration types.

public abstract class Enum<E extends Enum<E>> 
					extends Object 
					implements Comparable<E>, Serializable {
    
}

It means that all enums are comparable and serializable implicitly. Also, all enum types in Java are singleton by default.

As noted all enums extends java.lang.Enum, so enum cannot extend any other class because Java does not support multiple inheritance this way. But enums can implement any number of interfaces.

5. Compare enums

All enums are by default comparable and singletons as well. It means you can compare them with equals() method, even with "==" operator.

Direction east = Direction.EAST;
Direction eastNew = Direction.valueOf("EAST");

System.out.println( east == eastNew );           //true
System.out.println( east.equals( eastNew ) );    //true

You can compare enum types using '==' operator or equals() method, because enums are singlton and comparable by default.

6. Enum collections – EnumSet and EnumMap

Two classes have been added to java.util package in support of enums – EnumSet (a high-performance Set implementation for enums; all members of an enum set must be of the same enum type) and EnumMap (a high-performance Map implementation for use with enum keys).

6.1. java.util.EnumSet

EnumSet class is defined as follows:

public abstract class EnumSet<E extends Enum<E>> 
						extends AbstractSet<E> 
						implements Cloneable, Serializable {
  
}

A specialized 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.

6.1.1. EnumSet Example

public class Test 
{
   public static void main(String[] args) 
   {
     Set enumSet = EnumSet.of(  Direction.EAST,
                                Direction.WEST,
                                Direction.NORTH,
                                Direction.SOUTH
                              );
   }
 }

Like most collection implementations EnumSet is not synchronized. If multiple threads access an enum set concurrently, and at least one of the threads modifies the set, it should be synchronized externally.

null elements are not permitted. Also, these sets guarantee the ordering of the elements in the set based on their order in the enumeration constants is declared. Performance and memory benefits are very high in comparison to a regular set implementation.

6.2. java.util.EnumMap

EnumMap is declared as:

public class EnumMap<K extends Enum<K>,V> extends AbstractMap<K,V> implements Serializable, Cloneable {
  
}

A specialized Map implementation for use with enum type keys. Also, 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.

Like EnumSet, null keys are not permitted and is not synchronized as well.

6.2.1. EnumMap Example

public class Test 
{
  public static void main(String[] args)
  {
    //Keys can be only of type Direction
    Map enumMap = new EnumMap(Direction.class);

    //Populate the Map
    enumMap.put(Direction.EAST, Direction.EAST.getAngle());
    enumMap.put(Direction.WEST, Direction.WEST.getAngle());
    enumMap.put(Direction.NORTH, Direction.NORTH.getAngle());
    enumMap.put(Direction.SOUTH, Direction.SOUTH.getAngle());
  }
}

7. Summary

  1. enums are implicitly final subclasses of java.lang.Enum class
  2. if an enum is a member of a class, it’s implicitly static
  3. new keyword can not be used to intialize an enum, even within the enum type itself
  4. name() and valueOf() methods simply use the text of the enum constants, while toString() method may be overridden to provide any content, if desired
  5. for enum constants, equals() and "==" evaluates to same result, and can be used interchangeably
  6. enum constants are implicitly public static final
  7. the order of appearance of list of enum constants is called their “natural order“, and defines the order used by other items as well : compareTo() method, iteration order of values in EnumSet, EnumSet.range().
  8. Enum constructors should be declared as private. The compiler allows non private constructors, but this seems misleading to the reader, since new can never be used with enum types.
  9. Since these enumeration instances are all effectively singletons, they can be compared for equality using identity ("==").
  10. you can use enum in switch statement like int or char primitive data type

In this article, we explored the Java enum from the language basics to more advanced and interesting real-world use cases.

Happy Learning !!

References:

SO Thread
Enum Java Doc
Java 1.5 enumeration

Was this post helpful?

Join 7000+ Fellow Programmers

Subscribe to get new post notifications, industry updates, best practices, and much more. Directly into your inbox, for free.

33 thoughts on “Java enum tutorial”

  1. final class Direction extends Enum
    {
    public final static Direction EAST = new Direction();
    public final static Direction WEST = new Direction();
    public final static Direction NORTH = new Direction();
    public final static Direction SOUTH = new Direction();
    }

    I don’t understand this. How can enums be self-referential?
    Every declared new Direction() should AGAIN have 4 member variables, yet it has only one value.

    Reply
  2. Hi Lokesh,

    I am always confuse with when to use enum class and when to use Constant class.
    As you defined basic DIRECTION(enum) above which holds the definition of direction(EAST, WEST, NORTH, SOUTH).
    Similarly I can create my Direction class(below) which have EAST,WEST,NORTH,SOUTH constants.

     
    public class Direction {
    	public static final String EAST = "East";
    	public static final String WEST = "West";
    	public static final String NORTH = "North";
    	public static final String SOUTH = "South"; 
    }
    

    Please elaborate which approach is good and when should we use enum and when should we use Constant class.

    Thanks

    Reply
    • Hi,

      The above works as well. But, it would difficult to list the enum declarations from above Class. Where as I can retrieve all enum declarations using EnumSet.allOf(Direction.class) as explained by Lokesh.

      So, you could decide based on your requirement and usage.

      Reply
    • Hello Praveen,

      I think in any scenario where you want to restrict the number of values, then enum will be much better than constant String because:

      1. With above approach you wont be able to restrict user until you validate first.
      2. You can design your APIs in such a way that they work with enum and hence restrict values, as String can take any kind of value.
      3. With enums you can use == for comparison which is also null safe as compared to equals.
      4. Its very easy to extend you logic with enums. So, for example, you can easily extend enum to EAST to accept both “East” and “E” as valid values but this wont be straight forward with String.
      5. You can extend enums with util methods and then use those util methods in code, so future changes will only need update to util methods without impacting the caller of your API.

      Hope it clarifies a bit.

      Regards
      Vishal Sinha

      Reply
  3. Hi Lokesh,

    Let’s say there is Color enum with RED, GREEN, YELLOW and there is class Marks with color enum as its property. When I fetch Marks object from database I always pass the List Of markDto object in the order RED, GREEN, YELLOW irrespective of its corresponding values. By saying Corresponding values I meant I don’t care which color’s value is High or low. All I want is to fix the order RED, GREEN, YELLOW. Because from database it doesn’t guarantee the order but I have send the list to UI layer with above mentioned order.

    Please tell me how can I achieve it?

    Reply
  4. Hi Lokesh,

    If in database table, a column is enum type for example ( `language_type` enum(‘ENGLISH’,’HINDI’) DEFAULT NULL), then how can we insert data in this column using java hibernate entity class. I mean what would be the data type in java class. It should java enum type or java string type. Can we just use java string type?

    Reply
  5. Overall great and informative post about enums. Please, correct:

    enums extends java.lang.Enum

    that it somebody knows the exact angle

    public class EnumMap,V>

    Reply
  6. i have created an enum class, and have exposed an api to add certain objects to a tree map. this is handled in another class. so basically in my api i call this method to add the object.

    the problem i am facing while trying to write junit test cases for this scenario.. i am not able to add multiple objects . its a creating a new list each time when i create a new object or i change some value in the existing object and send it again.

    Reply
  7. Hi Lokesh,
    I am always confused with the values() API of enum. I mean Enum class don’t have this API.
    Java Docs says
    “The compiler automatically adds some special methods when it creates an enum. For example, they have a static values method that returns an array containing all of the values of the enum in the order they are declared. This method is commonly used in combination with the for-each construct to iterate over the values of an enum type.”

    I mean what you think what is the reason behind such a design, Why it was not designed simple rather then adding it by compiler

    Reply
    • Himansu, This is really a very good question. Let me put my take on this approach.

      Method values() is a static method which is added at instance level (i.e. each concrete enum). Where possibly we can add a static method such that it is available to all concrete instances without having reference in super class (i.e. Enum class)? Not possible by normal semantics of core java. Please remember that values() is not available for Enum class level.

      I believe that it was the only way for java designers to add a static method to concrete instances of enum. What’s your thoughts?

      Reply
  8. I need to convert from one Enum to another Enum. Would an EnumMap be the best way ? If so, would I put this EnumMap in a 3rd Enum declaration ? Or where would I place the EnumMap ? In my main class ?

    Reply
      • Thanks for helping Lokesh. Yes, an example would make the question more clear:

        I need to convert from Material.X to InventoryType.X …But notice that not all types can be converted. Only about 13 of them can actually make the conversion. I was curious about what some of the different approaches would be to do it.

        I used your EnumMap to do it (just haven’t been able to test it out yet). Here’s my class so far. This class handles the conversion: BaseType.java

        Would you do it this way ? Or maybe do string manipulation? Or another way? Thanks!

        Reply
        • To me, it looks good. Only thing, I will not make this public. Even if somebody need reference of it then return a readonly copy through some getter method.

          public static EnumMap<Material, InventoryType> emap = new EnumMap(Material.class);

          Reply
          • Sorry, I refactored the project yet again (which broke the link). This time, I refactored to default maven directory structure in order to use the /test/ folder to add any unit tests.

            If anyone was curious about the code related to my question, here it is BaseType.java

  9. This is a brilliant pattern – I had never thought about doing reverse lookups on an enum, until now!!! Handy … 🙂

    BTW, the code is missing a cast to “(Direction)” against the return value of static method “Direction get(int angle)”. However, rather than using that cast (Java 1 onwards), the better solution would be to make use of generics (Java 5 onwards), which is ideal because the “int” for the angle will be auto-boxed into the Integer type within the Map.

    i.e. change this:
    private static final Map lookup = new HashMap();
    to this:
    private static final Map lookup = new HashMap();
    then the cast is not needed.

    Reply

Leave a Comment

HowToDoInJava

A blog about Java and its related technologies, the best practices, algorithms, interview questions, scripting languages, and Python.