How does java support java enum constant instance variables?

I know that in Java, constant is enum

implicit static final

. But a class enum

can have an instance variable say size. Therefore, each constant enum

will have a copy "size". What is the equivalent Java code for this? I mean, what "seems to be" a static constant is enum

using a non-static instance variable, which is usually not possible?

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 that 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 reiterate my question, I'm interested in compiler implementation of the script. I already know how to use enums. I'm looking for what the compiler does? (so, for example, if I just write A, the compiler translates it to "public static final Member A." I want to know how the compiler gives a copy of the size for each A, B, C.

+3


source to share


2 answers


I mean it "seems" a static enum constant is using a non-static instance variable, which is not normal?

This is absolutely possible: each variable static

in enum

is an independent object, complete with its instance variables. The instance is static

in enum

, but it does not make the context of the instance itself a static context.



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

      

+3


source


I mean it "seems" a static enum constant is using a non-static instance variable, which is not normal?

What I meant by "usually" was that in classes other than enum, a static variable cannot access non-static variables

Still true: a static variable cannot access non-static variables. In your example code, you are not doing this: there is no static variable available for non-static variables.

A constructor is Members(int size)

not in a static context (a constructor never exists). A, B and C are all instances of the enumeration type Members

, and when these instances are created, the constructor is called with a parameter size

. Once constructed, these objects will be treated as static constant values.

Maybe another example might 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 persistent object with a non-static field name

. This is similar to a Members.A

static persistent object with a non-static field size

.

I want to know how the compiler gives a copy of the size for each A, B, C.

Just like it passes constructor parameters to any object.

You can read all about enums in the docs .

+2


source







All Articles