When are these class and subclass static blocks executed (for an Enum)?

回眸只為那壹抹淺笑 提交于 2019-12-12 12:26:29

问题


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); 
     }
}

回答1:


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.




回答2:


"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.




回答3:


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:

  • Effective Java
  • Java Language Specification - Example 8.9.2-1


来源:https://stackoverflow.com/questions/6827987/when-are-these-class-and-subclass-static-blocks-executed-for-an-enum

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!