Enumeration

Aim

  • Provide Options

Enum vs Class

  • Type Safety

public static final int SIZE_SMALL  = 1;
public static final int SIZE_MEDIUM = 2;
public static final int SIZE_LARGE  = 3;

public void setSize(int newSize) { ... }

obj.setSize(15); // Compiles but likely to fail later
public enum Size { SMALL, MEDIUM, LARGE };

public void setSize(Size s) { ... }

obj.setSize( ? ); // Can't even express the above example with an enum
  • Able to use switch

public enum Size { SMALL, MEDIUM, LARGE };
Size test = Size.SMALL;
switch(test){
    case SMALL:
        System.out.println("small");
        break;
    case MEDIUM:
        System.out.println("Medium");
        break;
    case LARGE:
        System.out.println("Large");
        break;
    default:
        break;
}        
// Output: small
  • Can be extended

public enum Standard {
    A(2,"nice"),B(6,"good"),C(4,"failed");
    
    private Integer key;
    private String comment;
    
    Standard(Integer key,String comment){
        this.key = key;
        this.comment = comment;
    }

    public Integer getKey() {
        return key;
    }

    public void setKey(Integer key) {
        this.key = key;
    }

    public String getComment() {
        return comment;
    }

    public void setComment(String comment) {
        this.comment = comment;
    }
}
  • Can be Override

public interface GradeAction {
   public Integer getScore();
   public String getDescription();
}
public enum MathGrade implements GradeAction {
    A, B, C, D;

    @Override
    public Integer getScore() {
        Integer score;
        switch (this) {
            case A:
                score = 90;
                break;
            case B:
                score = 80;
                break;
            case C:
                score = 70;
                break;
            case D:
                score = 60;
                break;
            default:
                score = 0;
                break;
        }
        return score;
    }

    @Override
    public String getDescription() {
        switch (this) {
            case A:
                return "優異";
            case B:
                return "佳";
            case C:
                return "良好";
            case D:
                return "普通";
            default:
                return "--";
        }
    }
}

Method

  • ValueOf

System.out.println(Standard.valueOf("A")==Standard.A);
//true
  • Ordinal

public enum Size { SMALL, MEDIUM, LARGE };
System.out.println(Size.SMALL.ordinal()); // 0
System.out.println(Size.MEDIUM.ordinal()); // 1
System.out.println(Size.LARGE.ordinal()); // 2
  • CompareTo

public enum Size { SMALL, MEDIUM, LARGE };
System.out.println(Size.SMALL.compareTo(Size.MEDIUM)); // -1
System.out.println(Size.SMALL.compareTo(Size.LARGE)); // -2 
System.out.println(Size.LARGE.compareTo(Size.SMALL)); // 2
  • toString and name (convert enum into string type)

public enum Size { SMALL, MEDIUM, LARGE };
System.out.println(Size.SMALL.toString()); // "SMALL" 
System.out.println(Size.SMALL.name());  // "SMALL"
System.out.println(Size.SMALL.toString()==Size.SMALL.name()); //true

Last updated

Was this helpful?