Reputation: 848
I know that in Java an enum
constant is implicitly static final
variable. But the enum
class can have an instance variable say size. So each enum
constant will have a copy of 'size'.
What is the equivalent Java code for this? I mean it "seems" the static enum
constant is using a non-static instance variable which is not possible normally?
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.
Upvotes: 3
Views: 2989
Reputation: 726509
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. The instance is static
in the 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; }
}
Upvotes: 3
Reputation: 124646
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).
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.
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
.
This is just like Members.A
, a static constant object with a non-static field size
.
I want to know how compiler gives a copy of size to each A,B,C.
Exactly the same way as it passes constructor parameters to any object.
You can read all about enums in the docs.
Upvotes: 2