Share on Google+Share on Google+

ENUM Types

Advertisement
In this SCJP section ,you will learn about ENUM Types of java

ENUM Types

A Enum type consist of a fixed set of constants. In java, you define an enum type by using the enum keyword. For example, you would specify a months-of-year enum type as:

public enum Year{
 January,February,March,April,May,June,July,August,September,October,November,December
     
}


Example :

package SCJP;

enum Foods {

burger, frenchFries, , Coke,

}

public class EnumExample {

public static void main(String args[])

{

Foods fd;

fd = Foods.burger;

switch(fd) {

case burger:

System.out.println("You have decided to eat burger!");

break;

case frenchFries:

System.out.println("You have decided to eat frenchFries");

break;

case Coke:

System.out.println("You have decided to eat  Coke");

break;

case Dosa:

System.out.println("You have decided to eat  Dosa");

break;

default:

System.out.println("I don't wanna eat any thing.");

break;

}

}

}

Output :

C:\Program Files\Java\jdk1.6.0_18\bin>java EnumExample 

You chose burger!

Download Source Code

See Also : Enum Type

Advertisements

Advertisement

Posted on: July 5, 2010 If you enjoyed this post then why not add us on Google+? Add us to your Circles

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.