I'm trying to define a base class (SubStatus
) for an Enum.
When are the static
blocks called below? If they were classes rather than enums I believe they would be called after the class constructor is called?
But because they are Enum
s, aren't these more like static
classes to begin with? So perhaps the static blocks are execute when the container is loading the static instances?
SubStatus
public enum SubStatus
{
WAITING(0),
READY(1);
protected static final Map<Integer,SubStatus> lookup
= new HashMap<Integer,SubStatus>();
static {
for(SubStatus s : EnumSet.allOf(SubStatus.class))
lookup.put(s.getCode(), s);
}
protected int code;
protected SubStatus(int code) {
this.code = code;
}
public int getCode() { return code; }
public static SubStatus get(int code) {
return lookup.get(code);
}
}
Status
public enum Status extends SubStatus
{
SKIPPED(-1),
COMPLETED(5);
private static final Map<Integer,Status> lookup
= new HashMap<Integer,Status>();
static {
for(Status s : EnumSet.allOf(Status.class))
lookup.put(s.getCode(), s);
}
private int code;
private Status(int code) {
this.code = code;
}
public int getCode() { return code; }
public static Status get(int code) {
return lookup.get(code);
}
}