enum java type

Enum is provided as a new data type to provide the predefined values (constants) for a variable. The Enum data type is defined in java using the enum keyword. The enum data type fields are in uppercase letters being constant values.

The enum data types can be helpful in supporting the different values for a particular attribute . For instance , an application goes through the life cycle and maintains differnet values for each user action which results into the change of the status for the application. So, we can have the enum data type for the application status as given below:

public enum Status {

INPROGRESS,UPDATED,PENDING_FOR_APPROVAL,APPROVED,ERROR,RE_PROCESSED

}

difference between enum data type and constants in java

1, The enum data type is type Safe and provides limited set of values

2. The enum data type proivdes the listing of all constants in the predefined enum data type whereas constans store single values and thus requires as many of constants as the values to be maintained

public enum Status {

INPROGRESS,UPDATED //enum data type values

}

public static final String INPROGRESS = “INPROGRESS”; //constant value
public static final String UPDATED = “UPDATED”; //constant value

java enum data type example

The below java program provides the usage of enum ApplicationStatus to get the different statuses from the enum data type.

package core.java.oracleappshelp.basics;
public class StatusEnumExample {
public enum ApplicationStatus{
	INPROGRESS,UPDATED,PENDING_FOR_APPROVAL,APPROVED,ERROR,RE_PROCESSED
}
    public void ValidateStatus(String ApplicationStatus) {
        switch (ApplicationStatus) {
            case "INPROGRESS":
                System.out.println("Application Status is changed to INPROGRESS.");
                break;
                    
            case "UPDATED":
            	System.out.println("Application Status is changed to UPDATED.");
                break;
                         
            case "PENDING_FOR_APPROVAL": 
            	System.out.println("Application Status is changed to PENDING_FOR_APPROVAL.");
                break;
            case "APPROVED":
            	System.out.println("Application Status is changed to APPROVED.");
                break;
                    
            case "ERROR":
            	System.out.println("Application Status is changed to ERROR.");
                break;
                         
            case "RE_PROCESSED": 
            	System.out.println("Application Status is changed to RE_PROCESSED.");
                break;      
                
            default:
                System.out.println("Application Status value cannot be evaluated.....");
                break;
        }
    }
    
    public static void main(String[] args) {
    	StatusEnumExample status = new StatusEnumExample();
    	status.ValidateStatus("INPROGRESS");
    	status.ValidateStatus("UPDATED");
    	status.ValidateStatus("PENDING_FOR_APPROVAL");
    	status.ValidateStatus("APPROVED");
    	status.ValidateStatus("ERROR");
    	status.ValidateStatus("RE_PROCESSED");
    	
    }
}

java enum data type program output:

Application Status is changed to INPROGRESS.
Application Status is changed to UPDATED.
Application Status is changed to PENDING_FOR_APPROVAL.
Application Status is changed to APPROVED.
Application Status is changed to ERROR.
Application Status is changed to RE_PROCESSED.

package core.java.oracleappshelp.basics;
public class StatusEnumExample {
public enum ApplicationStatus{
	INPROGRESS,UPDATED,PENDING_FOR_APPROVAL,APPROVED,ERROR,RE_PROCESSED
}
   public static void main(String[] args) {
    	StatusEnumExample status = new StatusEnumExample();
    	//printing all the values of application status enum 
         ApplicationStatus  statusEnum[] = ApplicationStatus.values();
        System.out.println("Value of application status enum : ");
        for(ApplicationStatus statusValue: statusEnum) {
           System.out.println(statusValue);
        }
    	
    }
}

Java program output:

Value of application status enum :
INPROGRESS
UPDATED
PENDING_FOR_APPROVAL
APPROVED
ERROR
RE_PROCESSED

Java Enumeration Interface

The java.util package provides the Enumeration class which implements the Enum Interface. The below given is the example for the Vector using Enumeration usage.

package core.java.oracleappshelp.basics;
import java.util.Enumeration;
import java.util.Vector;
public class EnumerationExample {
	 public static void main(String args[]) {
	      //instantiating a Vector
	      Vector<String> vec = new Vector<String>( );
	      //Populating the vector
	      vec.add("INPROGRESS");
	      vec.add("UPDATED");
	      vec.add("PENDING_FOR_APPROVAL");
	      vec.add("APPROVED");
	      vec.add("ERROR");
	      vec.add("RE_PROCESSED");
	      //Retrieving the elements using the Enumeration
	      Enumeration<String> status = vec.elements();
	      while(status.hasMoreElements()) {
	         System.out.println(status.nextElement());
	      }
	   }
}

Java enumeration program implementation using vector:

INPROGRESS
UPDATED
PENDING_FOR_APPROVAL
APPROVED
ERROR
RE_PROCESSED