ENUM Types


 

ENUM Types

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

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;

}

0

}

}

Output :

1

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

You chose burger!

Download Source Code

2

See Also : Enum Type

Ads