3

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 Enums, 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); 
     }
}
Ben
  • 31
  • 1

3 Answers3

4

The static block is processed when the first call to you enum has been made but after all enums values have been created. Btw, your code won't work. There is no inheritance in Java enums. Resort to interfaces if you need something like this.

Michael-O
  • 18,123
  • 6
  • 55
  • 121
1

This is the order:

  1. Enum Constructors

    The enum constructors are ran before the static fields are initialized. This is required because static methods need to have access to all the enum values (instances). Enum values are implicitly assigned to static fields.

  2. Static initializers

    Then the static initializers, and static blocks are called in order of occurrence.

References:

Community
  • 1
  • 1
YoYo
  • 9,157
  • 8
  • 57
  • 74
1

"class constructor" is a colloquial term, the name, according to the specification is static initializer. On a related note: there's no such thing as a "static class" in Java ;-)

Enums in Java are classes. They have all the same trimmings and behaviour (they can even have mutable fields, although that's usually a bad idea). They are only restricted in their type hierarchy (they must not explicitly extend another class and may not be explicitly extended themselves) and in construction (their enum values are the only instances that can ever be created).

Therefore static initializer blocks are executed the same way they are for normal classes: when the class is loaded/initialized. That usually happens when it is first accessed.

Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614