Serialization mechanism ensures that new enum instances are not created on serialization and de-serialization (ensuring singleton behaviour). In a distributed environment when an enum is serialized from one JVM to another JVM, enum's state is not serialized. Hence, it is not encouraged to have state with enums which is updated by the applications.
You can only have constant attributes that are initialized by the constructor.
Eg.
import java.io.Serializable;
public class Status {
public enum Status implements Serializable{
STATUS_OPEN(0, "open"),
STATUS_STARTED(1, "started"),
STATUS_INPROGRESS(2, "inprogress"),
STATUS_ONHOLD(3, "onhold"),
STATUS_COMPLETED(4, "completed"),
STATUS_CLOSED(5, "closed");
private final int status;
private final String description;
Status(int aStatus, String desc){
this.status = aStatus;
this.description = desc;
}
public int status(){
return this.status;
}
public String description(){
return this.description;
}
}
}
Excellent article , you have indeed covered topic in details with benchmarking result and graphics.
ReplyDeleteThanks
Javin
Top 10 Java Serialization Interview Question
Thanks Javin, after reading article and then reading questions really adds the concepts to the memory.
ReplyDelete