Advertisement
DulcetAirman

Enum in Java

Jan 22nd, 2013
85
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. This is to demonstrate how enums are just syntactic sugar. But this is not complete. What about hashValue, equals, compareTo, name, toString?
  3. */
  4. ---------------------------------------------
  5. package com.example;
  6.  
  7. import java.awt.Color;
  8.  
  9. public final class MyEnum {
  10.     public static final MyEnum ENUM_APPLE = new MyEnum(Color.GREEN);
  11.     public static final MyEnum ENUM_BANANA = new MyEnum(Color.YELLOW);
  12.     private final Color color;
  13.  
  14.     private MyEnum(final Color _color) {
  15.         this.color = _color;
  16.     }
  17.  
  18.     public Color getColor() {
  19.         // OK, because Color is immutable. Otherwise return a clone:
  20.         return color;
  21.     }
  22. }
  23. ---------------------------------------------
  24. public enum MyEnum {
  25.     ENUM_APPLE (Color.GREEN), ENUM_BANANA (Color.YELLOW);
  26.     private final Color color;
  27.    
  28.     private MyEnum(final Color _color) {
  29.         this.color = _color;
  30.     }
  31.  
  32.     public Color getColor() {
  33.         // OK, because Color is immutable. Otherwise return a clone:
  34.         return color;
  35.     }
  36. }
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement