An enumeration of apple varieties. : Enum « Language Basics « Java






An enumeration of apple varieties.

An enumeration of apple varieties.
  
enum Apple {  
  A, B, C, D, E 
} 
 
public class EnumDemo { 
  public static void main(String args[])  
  { 
    Apple ap; 
 
    ap = Apple.C; 
 
    // Output an enum value. 
    System.out.println("Value of ap: " + ap); 
    System.out.println(); 
 
    ap = Apple.B; 
 
    // Compare two enum values. 
    if(ap == Apple.B)  
      System.out.println("ap conatins GoldenDel.\n"); 
 
    // Use an enum to control a switch statement. 
    switch(ap) { 
      case A: 
        System.out.println("A is red."); 
        break; 
      case B: 
        System.out.println("B is yellow."); 
        break; 
      case C:  
        System.out.println("C is red."); 
        break; 
      case D: 
        System.out.println("D is red."); 
        break; 
      case E: 
        System.out.println("E is red."); 
        break; 
    } 
  } 
}


           
         
    
  








Related examples in the same category

1.How to use enum
2.Enum with switch statement
3.Enum and GenericEnum and Generic
4.Media enumeration constantsMedia enumeration constants
5.Use an enum constructor.
6.Switching on Enum and Adding Methods to an Enum
7.Use the built-in enumeration methods. Use the built-in enumeration methods.
8.Use an enum constructor, instance variable, and method. Use an enum constructor, instance variable, and method.
9.Java enum: Demonstrate ordinal(), compareTo(), and equals().Java enum: Demonstrate ordinal(), compareTo(), and equals().
10.Java enum: Creating an EnumJava enum: Creating an Enum
11.Java enum: EnumMap and Ant statusJava enum: EnumMap and Ant status
12.Java enum: Enum inside class
13.Java enum and varargs
14.This program demonstrates enumerated types
15.Shuffler Enum