forum

Home / DeveloperSection / Forums / java support instance variables of java enum constants

java support instance variables of java enum constants

Anonymous User198416-Oct-2014
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?
enumMembers{
        A(1),B(2),C(3);  // I have 3 enum constants here
 
        privateint size;
        Members (int size) {
            //System.out.println("Initializing var with size = "+size);
        }
    }

Equivalent code I know so far:

public final classMember extends Enums<Members> {
        publicstatic 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.


Updated on 16-Oct-2014
I am a content writter !

Can you answer this question?


Answer

1 Answers

Liked By