这些类和子类静态块何时执行(对于枚举)?

发布于 2024-11-26 12:05:55 字数 1347 浏览 0 评论 0原文

我正在尝试为枚举定义基类 (SubStatus)。

下面什么时候调用static块?如果它们是类而不是枚举,我相信它们会在调用类构造函数之后被调用?

但因为它们是Enum,所以它们一开始不是更像static类吗?那么静态块可能是在容器加载静态实例时执行的吗?

子状态

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

状态

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦中的蝴蝶 2024-12-03 12:05:55

当第一次调用枚举时但创建了所有枚举值之后,将处理静态块。顺便说一句,你的代码将无法工作。 Java 枚举中没有继承。如果您需要这样的东西,请使用接口。

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.

余厌 2024-12-03 12:05:55

“类构造函数”是一个口语术语,其名称,根据规范是 静态初始值设定项。相关说明:Java 中不存在“静态类”这样的东西;-)

Java 中的枚举类。它们具有相同的修饰和行为(它们甚至可以具有可变字段,尽管这通常是一个坏主意)。它们仅在类型层次结构(它们不得显式扩展另一个类,并且不能显式扩展自身)和构造(它们的枚举值是唯一可以创建的实例)方面受到限制。

因此,静态初始化块的执行方式与普通类相同:加载/初始化类时。这通常发生在第一次访问时。

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

真心难拥有 2024-12-03 12:05:55

这是顺序:

  1. 枚举构造函数

    枚举构造函数在静态字段初始化之前运行。这是必需的,因为静态方法需要访问所有枚举值(实例)。枚举值隐式分配给静态字段。

  2. 静态初始化器

    然后按照出现的顺序调用静态初始化器和静态块。

参考文献:

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:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文