Monday, March 9, 2009

Java enums

Listed below are a few highlights about Java enums. It is followed by an example.
- enum is a type in Java
- used to represent a fixed set of constants. so the enum constants are implicitly public static final.
- is Serializable
- implements Comparable
- is NOT Cloneable
- implicitly extends the class java.lang.Enum so they cannot extend any other class or enum. So is implicitly final.
- cannot be abstract
- can have the access modifiers public or default.
- can be defined outside the class or as a class member. They cannot be defined inside methods. Enums defind inside a class are implicitly static.
- can have one or more constructors but they cannot be called explicitly. So better declare them private.
- cannot be instantiated using new operator.
- can have methods including abstract methods. An enum constant can override any number of available methods. Abstract methods have to be implemented by each and every constant of that enum.
- name(), valueOf() and defualt implementation of the toString() methods is to return the name of the enum constant. The enum constants can override toString() method to return something else.
- can be used as an operand in the instanceof operator
- enum constants can be used in switch statements.
- '==' and equals yield the same results for enum constants.
- natural order of the enum constants is the way they are defined and is used by compareTo(), values(), EnumSet.range().
- ordinal() returns the natural ordering of the enum constants starting at the index at 0 for the first constant.
- cannot be generic

public class MyEnumType {

  //Simple enum
  public enum TrainType {
    STEAM,DIESEL //; is optional
  }
  
  //Complex enum
  public enum ThomasAndFriends {
    
    //Enums can have constructors
    THOMAS(1,"blue"), EDWARD(2,"blue"),GORDON(4,"blue"),
    DAISY(9,"black"){
      //Can override method
      @Override public TrainType getTrainType() {
        return TrainType.DIESEL;
      }
    },
    JAMES(5,"red"),PERCY(6,"green");
    
    private int engineNumber;
    private String color;
    
    //Constructor's access can be private or default.
    ThomasAndFriends(int engineNumber, String color) {
      this.engineNumber = engineNumber;
      this.color = color;
    }
    
    public int getEngineNumber(){
      return engineNumber;
    }
    
    public String getColor(){
      return color;
    }
    
    public TrainType getTrainType(){
      return TrainType.STEAM;
    }
    
  }
  
  public static void main(String[] args) {
    
    //Walk through the enumeration and print values
    for (ThomasAndFriends tf: ThomasAndFriends.values()) {
      //Enums have ordinals but do not use them. Code dosent 
      //scale, instead use constructors to specify values.
      System.out.println("Name: " + tf + 
              " EngineNumber: " + tf.getEngineNumber() +
              " Enum Ordinal: " + tf.ordinal() +
              " Color: " + tf.getColor() +
              " TrainType: " + tf.getTrainType());  
    }
    
  }

}

OUTPUT:
Name: THOMAS EngineNumber: 1 Enum Ordinal: 0 Color: blue TrainType: STEAM
Name: EDWARD EngineNumber: 2 Enum Ordinal: 1 Color: blue TrainType: STEAM
Name: GORDON EngineNumber: 4 Enum Ordinal: 2 Color: blue TrainType: STEAM
Name: DAISY EngineNumber: 9 Enum Ordinal: 3 Color: black TrainType: DIESEL
Name: JAMES EngineNumber: 5 Enum Ordinal: 4 Color: red TrainType: STEAM
Name: PERCY EngineNumber: 6 Enum Ordinal: 5 Color: green TrainType: STEAM

No comments:

Post a Comment