Home Help Java E Enum Data Types

Related Tutorials


 
 

Share on Google+Share on Google+

Enum Data Types

Advertisement
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 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.

 

Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: October 6, 2010

Related Tutorials

Discuss: Enum Data Types   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0
DMCA.com