Java

Java – Enum Tutorial With Examples

Enums are a unique class of data types that are essentially collections (sets) of constants. In this article, we’ll learn how to utilize enums in Java and what situations could call for their usage.

This is how we define Enum

public enum Directions{
  EAST, 
  WEST, 
  NORTH, 
  SOUTH
}

Here, we have an enum type variable called Directions, which is made up of the four variables EAST, WEST, NORTH, and SOUTH.

How is a value assigned to an enum type?

Directions dir = Directions.NORTH;

Dir is a variable of the Directions type (that is a enum type). Out of the four available values, this variable may have any value (EAST, WEST, NORTH, SOUTH). It is set to NORTH in this instance.

Use of Enum types in if-else statements

This is how an enum variable may be used in an if-else logic statement.

/* 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 only an example of how to utilize enums. You would be able to construct your own logic depending on the demand if you understood the main component and the fundamentals.

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

The following example shows how to utilize 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

How to iterate through an Enum variable

class EnumDemo
{
    public static void main(String[] args) {
    	for (Directions dir : Directions.values()) {
    	    System.out.println(dir);
    	}
    }
}

Enum Fields and Methods

Before we go into the details, let’s look at an example:

public enum Directions{
  EAST ("E"), 
  WEST ("W"), 
  NORTH ("N"), 
  SOUTH ("S")
  ; 
  /* Important Note: Must have semicolon at
   * the end when there is a enum field or method
   */
  private final String shortCode;
	  
  Directions(String code) {
      this.shortCode = code;
  }
	  
  public String getDirectionCode() {
      return this.shortCode;
  }
}
public class EnumDemo
{
    public static void main(String[] args) {
    	Directions dir = Directions.SOUTH;
    	System.out.println(dir.getDirectionCode());
    	Directions dir2 = Directions.EAST;
    	System.out.println(dir2.getDirectionCode());
    }
}

Output:

S
E

Related Articles

Leave a Reply

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

Back to top button