mkv

Untitled

mkv
Mar 27th, 2012
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.39 KB | None | 0 0
  1. // quick introduction to java enums
  2. // for eggmun @ rizon
  3. // by ed @ rizon
  4.  
  5. public class Eggmun01 {
  6.    
  7.     public static void main(String[] args) {
  8.         // trick to avoid having to static everything
  9.         new Eggmun01().rin(args);
  10.     }
  11.    
  12.     enum Color
  13.     {
  14.         RED, GREEN, BLUE, YELLOW
  15.     }
  16.    
  17.     // our non-static main()
  18.     public void rin(String[] args)
  19.     {
  20.         Color one = Color.BLUE;
  21.         Color two = Color.YELLOW;
  22.        
  23.        
  24.        
  25.         // first example; most frequent usage
  26.        
  27.         if (one == Color.BLUE)
  28.         {
  29.             System.out.println("Color one is blue");
  30.         }
  31.         else
  32.         {
  33.             System.out.println("Color one is not blue");
  34.         }
  35.        
  36.        
  37.        
  38.         // second example; using an enum value as:
  39.         //  - a string (implicit typecasting)
  40.         //  - or int (explicit typecasting)
  41.        
  42.         System.out.println("Color one is " + one + " which is color " + one.ordinal() + " in the enum");
  43.        
  44.        
  45.        
  46.         // third example; comparing enum variables
  47.        
  48.         if (one == two)
  49.         {
  50.             System.out.println("The colors match");
  51.         }
  52.         else
  53.         {
  54.             System.out.println("The colors does not match");
  55.         }
  56.        
  57.        
  58.        
  59.         // fourth example; list colors in the enum
  60.        
  61.         Color[] yeah = Color.values();
  62.        
  63.         for (int i = 0; i < yeah.length; i++)
  64.         {
  65.             System.out.print(yeah[i] + ", ");
  66.         }
  67.        
  68.         System.out.println();
  69.        
  70.        
  71.        
  72.         // fifth example; echo color #n
  73.        
  74.         System.out.print("The 2nd color is " + Color.values()[2]);
  75.        
  76.        
  77.        
  78.         // HTH
  79.     }
  80. }
Advertisement
Add Comment
Please, Sign In to add comment