codescracker


java

Java Enumeration



« Previous Tutorial Next Tutorial »


An enumeration is a list of named constants. Even though Java offered some other features that provide somewhat similar functionality, like final variables, many programs still missed the conceptual purity of the enumerations, especially because enumerations are supported by lots other commonly used computer languages.

In their simplest form, Java enumerations appear similar to enumerations in the other computer languages. Nevertheless, this similarity may be only skin deep because, an enumeration defines a class type in Java. By making enumerations into classes, the capabilities of enumeration are greatly expanded. For instance, in Java, an enumeration can have methods, constructors, and instance variables. Therefore, although enumerations were several years in making. Java's rich implementation made them well worth the wait.

Create Enumeration in Java

An enumerations is created using the keyword enum. For example, following is a simple enumeration that lists the various apple varieties:

// here is an enumeration of several apple varieties
enum Apple { GodenDel, Winesap, Jonathan, Cortland }

The identifiers GodenDel, Winesap, and so on, are called as enumeration constants. Each is absolutely declared as public, static final member of the Apple. Moreover, their type is the type of enumeration in which they are declared, that is Apple in this case. Therefore, in Java, these constants are self-typed, in which "self" thinks of to the enclosing enumeration.

Declare Enumeration Variable in Java

Once you have defined an enumeration, you can create a variable of that type. Nevertheless, even though enumerations define a class type, you do not instantiate an enum using the new operator/keyword. Instead, you can declare and use an enumeration variable in much the same way as you do one of the primitive types. For instance, this declares apl as a variable of enumeration the type Apple :

Apple apl;

Assign Value to Enumeration Variable in Java

Because apl is of the type Apple, the only values that it can be assigned or can contain are those defined by enumeration. For instance, following assigns apl the value GoldenDel constant :

apl = Apple.GoldenDel;

Notice that the symbol GoldenDel is preceded by Apple.

How to Compare two Enumeration Constants ?

The two enumeration constants can be compared for equality by using the == relational operator. For instance, this statement compares the value present in apl with the RedDel constant:

if(apl == Apple.RedDel) // ...

An enumeration value can also be used to control the switch statement. Naturally, all of the case statements must use the constants from the same enum as that used by the switch expression. For instance, the following switch is perfectly valid :

/* use an enum to control the switch statement */
switch(apl)
{
	case GoldenDel:
	   // ...
	case Cortland:
	   // ...

Notice here that in the case statements, the names of the enumeration constants are used without being qualified by their enumeration type name, that is, Cortland is used, not Apple.Cortland. This is because the type of enumeration in the switch expression has already specified the enum type of the case constants. There is no need to qualify the constants in case statements with their enum type name. In fact, attempting to do so will cause a compilation error.

When an enumeration constant is displayed, such as in a statement println(), its name is output. For example, given this statement :

System.out.println(Apple.Cortland);

the name Cortland is displayed.

Java Enumeration Example

Here this program puts together all of the pieces and illustrates the Apple enumeration :

/* Java Program Example - Java Enumeration Example 
 * An enumeration of several apple varieties 
 */
 
 enum Apple { GoldenDel, Winesap, RedDel, Jonathan, Cortland }
 
 class EnumerationDemo
 {
     public static void main(String args[])
     {
         
         Apple apl;
         
         apl = Apple.GoldenDel;
         
         /* output an enum value */
         System.out.println("Value of apl : " + apl);
         System.out.println();
         
         apl = Apple.RedDel;
         
         /* compare the two enum values */
         if(apl == Apple.RedDel)
            System.out.println("apl contains RedDel\n");
         
         /* use an enum to control the switch statement */
         switch(apl)
         {
             case GoldenDel : 
                System.out.println("Golden Delicious is yellow");
                break;
             case Winesap :
                System.out.println("Winesap is red");
                break;
             case RedDel :
                System.out.println("Red Delicious is red");
                break;
             case Jonathan :
                System.out.println("Jonathan is red");
                break;
             case Cortland :
                System.out.println("Cortland is red");
                break;
         }
         
     }
 }

When the above Java program is compile and run, it will produce the following output:

java enumerations

« Previous Tutorial Next Tutorial »



Tools
Calculator

Quick Links
Signup - Login - Give Online Test