简体   繁体   English

java如何支持java enum常量的实例变量?

[英]How does java support instance variables of java enum constants?

I know that in Java an enum constant is implicitly static final variable. 我知道在Java中, enum常量是隐式static final变量。 But the enum class can have an instance variable say size. 但是enum类可以有一个实例变量,比如说大小。 So each enum constant will have a copy of 'size'. 所以每个enum常量都有一个'大小'的副本。 What is the equivalent Java code for this? 这个等效的Java代码是什么? I mean it "seems" the static enum constant is using a non-static instance variable which is not possible normally? 我的意思是“似乎”静态enum常量是使用非静态实例变量,这通常是不可能的?

enum Members{
    A(1),B(2),C(3);  // I have 3 enum constants here

    private int size;
    Members (int size) {
        //System.out.println("Initializing var with size = "+size);
    }
}

Equivalent code I know so far: 到目前为止我所知道的等效代码:

public final class Member extends Enums<Members> {
    public static final Members A;
    // ...
    // What happened to size? How do A,B,C get a copy of size?
}

Edit : To restate my question- I am interested in behind the scene implementation by compiler. 编辑:重述我的问题 - 我对编译器的场景实现感兴趣。 I already know how to use enums. 我已经知道如何使用枚举。 I am looking for what the compiler does? 我在寻找编译器的功能? (so for example, if I simply write A, the compiler translates it to "public static final Member A". I want to know how compiler gives a copy of size to each A,B,C. (例如,如果我只是编写A,编译器会将其转换为“public static final Member A”。我想知道编译器如何为每个A,B,C提供大小的副本。

I mean it "seems" the static enum constant is using a non-static instance variable which is not possible normally? 我的意思是“似乎”静态枚举常量是使用非静态实例变量,这通常是不可能的?

It is absolutely possible: each static variable in the enum is an object in its own right, complete with its instance variables. 绝对可能: enum中的每个static变量本身就是一个对象,并且包含其实例变量。 The instance is static in the enum , but it does not make the context of the instance itself a static context. 实例在enumstatic的,但它不会使实例本身的上下文成为静态上下文。

public final class Member extends java.lang.Enums<Members> {
    public static final Members A = new Member(1) {
        public String toString() { return "A:"+size; }
    };
    public static final Members B = new Member(2) {
        public String toString() { return "B:"+size; }
    };
    public static final Members C = new Member(3) {
        public String toString() { return "C:"+size; }
    };
    private final int size;
    protected Member(int size) { this.size = size; }
}

I mean it "seems" the static enum constant is using a non-static instance variable which is not possible normally? 我的意思是“似乎”静态枚举常量是使用非静态实例变量,这通常是不可能的?

What I meant by "normally" was that in non-enum classes a static variable can't access non static variables 我所说的“通常”是指在非枚举类中,静态变量无法访问非静态变量

It's still true: a static variable can't access non-static variables. 它仍然是真的:静态变量不能访问非静态变量。 In your example code, you don't do this: there is no static variable accessing non-static variables. 在您的示例代码中,您不这样做:没有静态变量访问非静态变量。

The constructor Members(int size) is not in a static context (a constructor never is). 构造函数Members(int size)不在静态上下文中(构造函数永远不会)。 A, B and C are all instances of the enum type Members , and when these instances are created, the constructor is called with the size parameter. A,B和C都是枚举类型Members所有实例,并且在创建这些实例时,将使用size参数调用构造函数。 Once constructed, these objects will be treated as static constant values. 构造完成后,这些对象将被视为静态常量值。

Perhaps another example can help: 也许另一个例子可以帮助:

class Person {
    private final String name;
    Person(String name) {
        this.name = name;
    }
}
class EmployeeDatabase {
    private static final Person CEO = new Person("Jack");
}

Here EmployeeDatabase.CEO is a static constant object with a non-static field name . 这里EmployeeDatabase.CEO是一个静态常量对象,具有非静态字段name This is just like Members.A , a static constant object with a non-static field size . 这就像Members.A ,一个具有非静态字段size的静态常量对象。

I want to know how compiler gives a copy of size to each A,B,C. 我想知道编译器如何为每个A,B,C提供大小的副本。

Exactly the same way as it passes constructor parameters to any object. 与将构造函数参数传递给任何对象的方式完全相同。

You can read all about enums in the docs . 您可以在文档中阅读有关枚举的所有内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM