Enum Data Types

Enum type is a type which consist of fixed set of constant fields. like direction and days includes values NORTH, SOUTH, EAST, and WEST and SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY and SATURDAY respectively.

Enum Data Types

Enum Data Types

     

Enum type is a type which consist of fixed set of constant fields. like direction and days includes values NORTH, SOUTH, EAST, and WEST and SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY and SATURDAY respectively. Since they are constants so we are taking these values into the uppercase letters.

 

Lets take an example of enum data types:

 

EnumTestResult.java

public class EnumTestResult {
  DaysOfWeek day;
  
  public EnumTestResult(DaysOfWeek day) {
  this.day = day;
  }
  
  public void howsday() {
  switch (day) {
  case MONDAY: System.out.println("Mondays are working days.");
 break;
  
  case THURSDAY: System.out.println
   ("Thursday are also working days.");
 break;
 
  case SATURDAY:
  case SUNDAY: System.out.println("Weekends are best.");
 break;
 
  default: System.out.println("Midweek days are so-so.");
 break;
  }
  }
  
  public static void main(String[] args) {
  EnumTestResult FirstDay = new EnumTestResult(DaysOfWeek.MONDAY);
  FirstDay.howsday();
  EnumTestResult ThirdDay = new EnumTestResult(DaysOfWeek.WEDNESDAY);
  ThirdDay.howsday();
  EnumTestResult FourthDay = new EnumTestResult(DaysOfWeek.FRIDAY);
  FourthDay.howsday();
  EnumTestResult SixthDay = new EnumTestResult(DaysOfWeek.SATURDAY);
  SixthDay.howsday();
  EnumTestResult SeventhDay = new EnumTestResult(DaysOfWeek.SUNDAY);
  SeventhDay.howsday();
  
  
  }
}

DaysOfWeek.java

public enum DaysOfWeek { 
  SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY 
}

Here is the Output:

C:\Documents and Settings\compaq 20\Desktop\Tutorials>java EnumTestResult
Mondays are working days.
Midweek days are so-so.
Midweek days are so-so.
Weekends are best.
Weekends are best.