An enum is a special type of data type which is basically a collection (set) of constants. In this tutorial we will learn how to use enums in Java and what are the possible scenarios where we can use them

This is how we define Enum
public enum Directions{
  EAST, 
  WEST, 
  NORTH, 
  SOUTH
}

Here we have a variable Directions of enum type, which is a collection of four constants EAST, WEST, NORTH and SOUTH.

How to assign value to a enum type?

Directions dir = Directions.NORTH;

The variable dir is of type Directions (that is a enum type). This variable can take any value, out of the possible four values (EAST, WEST, NORTH, SOUTH). In this case it is set to NORTH.

Use of Enum types in if-else statements

This is how we can use an enum variable in a if-else logic.

/* You can assign any value here out of
 * EAST, WEST, NORTH, SOUTH. Just for the
 * sake of example, I'm assigning to NORTH
 */
Directions dir = Directions.NORTH;  

if(dir == Directions.EAST) {
  // Do something. Write your logic
} else if(dir == Directions.WEST) {
     // Do something else
  } else if(dir == Directions.NORTH) {
     // Do something 
    } else {
        /* Do Something. Write logic for 
         * the remaining constant SOUTH
         */ 
      }
Enum Example

This is just an example to demonstrate the use enums. If you understand the core part and basics, you would be able to write your own logic based on the requirement.

public enum Directions{
	  EAST, 
	  WEST, 
	  NORTH, 
	  SOUTH
}
public class EnumDemo
{
   public static void main(String args[]){
	Directions dir = Directions.NORTH;  
	if(dir == Directions.EAST) {
	    System.out.println("Direction: East");
	} else if(dir == Directions.WEST) {
	    System.out.println("Direction: West");
	  } else if(dir == Directions.NORTH) {
	      System.out.println("Direction: North");
  	    } else {
		System.out.println("Direction: South");
	      }
   }
}

Output:

Direction: North
Use of Enum in Switch-Case Statements

Here is the example to demonstrate the use of enums in switch-case statements.

public enum Directions{
	  EAST, 
	  WEST, 
	  NORTH, 
	  SOUTH
}
public class EnumDemo
{
   Directions dir;
   public EnumDemo(Directions dir) {
      this.dir = dir;
   }
   public void getMyDirection() {
     switch (dir) {
       case EAST:
          System.out.println("In East Direction");
          break;
                    
       case WEST:
          System.out.println("In West Direction");
          break;
                         
       case NORTH: 
          System.out.println("In North Direction");
          break;
                        
       default:
          System.out.println("In South Direction");
          break;
     }
   }
    
    public static void main(String[] args) {
        EnumDemo obj1 = new EnumDemo(Directions.EAST);
        obj1.getMyDirection();
        EnumDemo obj2 = new EnumDemo(Directions.SOUTH);
        obj2.getMyDirection();
    }
}

Output:

In East Direction
In South Direction

 

Leave a Reply

Your email address will not be published. Required fields are marked *