A programmer's tale

Thursday, July 27, 2006

Tale of enum

enum keyword is new in Java 1.5. Here I'll try to tell it's tale.

Enum is a special kind of class in java 1.5. An enum may contain some values and each value is implicitly an enum of the same type in which it is defined. Since enum is a special type of class, we can use it as a class to some extend.

It will be clear if you take an example. Let us define:

public enum Day {

   MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY;

}

Here, MONDAY, TUESDAY etc. are all Day enum type. In simple case you can implement the same behavior with integer, other primitive or with classes like:

public class Day {

   public static final int MONDAY == 0;
    public static final int TUESDAY == 1;
    public static final int WEDNESDAY == 2;
    public static final int THURSDAY == 3;
   public static final int FRIDAY == 4;
public static final int SATURDAY == 5;
public static final int SUNDAY == 6;

}

But you can do the thing more safely and efficiently and compact way with enum.
Now like class we can also declare variables and define methods in an enum like:

public enum DayDesc {

MONDAY("Monday is the first day"),
TUESDAY("Tuesday is the second day"),
WEDNESDAY("Wednesday is the third day"),
THURSDAY("Thursday is the fourth day"),
FRIDAY("Friday is the fifth day"),
SATURDAY("Saturday is the sixth day"),
SUNDAY("Sunday is the seventh day");

private String info;

DayDesc(String info) {
this.info = info;
}

public String info() {
return info;
}
}

In this case the enum DayDesc has a constructor and every time MONDAY, TUESDAY is declared and defined with some String info the constructor is called like a class. You can use this as follow:

public static void main(String[] args) {
System.out.println(DayDesc.MONDAY.info());
System.out.println(DayDesc.THURSDAY.info());

// .... etc
}

Here the values MONDAY, TUESDAY, WEDNESDAY etc are all constants but the private member info is not. It is like any private attribute declared or defined in normal class. You can assume that the values are like public static final attributes of a normal class. Now we write a normal class that corresponds the above enum class:

public class ClassEnum {

public static final ClassEnum MONDAY = new ClassEnum("Monday is the first day");
public static final ClassEnum TUESDAY = new ClassEnum("Tuesday is the second day");
public static final ClassEnum WEDNESDAY = new ClassEnum("Wednesday is the third day");
public static final ClassEnum THURSDAY = new ClassEnum("Thursday is the fourth day");
public static final ClassEnum FRIDAY = new ClassEnum("Friday is the fifth day");
public static final ClassEnum SATURDAY = new ClassEnum("Saturday is the sixth day");
public static final ClassEnum SUNDAY = new ClassEnum("Sunday is the seventh day");

private String info;

private ClassEnum(String info) {
this.info = info;
}

public String info() {
return info;
}
}

It's obvious that this class also can be used as the previous enum class same way. Look, the constructor of this class is private. In enum class the constructor is implicitly private, if you try to declare it as protected or public it will give you compilation error. For this reason enum class cannot be instantiated outside the enum class itself by new operator. Moreover enum class cannot be cloned. Let us see how it is made. Every java class is implicitly a super class of the class java.lang.Object. But every enum class is implicitly a super class of the class java.lang.Enum. If you look at the class, you will see that the clone() method as follows:

protected final Object clone() throws CloneNotSupportedException {
throw new CloneNotSupportedException();
}

Hence you cannot clone it. Again the reflection and serialization is specially treated for enum class so that no duplicate instance can exist. One very interesting think is that the following two methods are only generated by the compiler for enum class:
values();
valueOf(String arg0);

First method returns an array of enumerated values of that type, then you can iterate through the values like :

for (DayDesc day : DayDesc.values())
    System.out.println(day);

Second method return enum type by a String. The argument arg0 must be same to one of the values defined in enum class otherwise a java.lang.IllegalArgumentException exception is thrown.

0 Comments:

Post a Comment

<< Home