Aaron
Aaron

Reputation: 999

The values of an enumerated type are static variables of that type. True?

The values of an enumerated type are static variables of that type. Far as i know, the variables are referenced, by the refererence variable but there is no new operator to instantiate an enum object. But is it like how you can initialize an array?

Is this true or false?

Upvotes: 2

Views: 110

Answers (2)

mhaller
mhaller

Reputation: 14232

Yes, the literals of an enum type are public static final variables.

Simplified, it looks like this:

public final enum FooEnum {
    A, B
}

public final class BarEnum {
    public static final BarEnum A = new BarEnum();
    public static final BarEnum B = new BarEnum();
}

In reality, there is a bit more stuff there, such as the list of all enumeration literals, a String identifier (the enum value knows its name), an ordinal number and a private Constructor to prevent instantiation (all omitted for clarity of code since question was only about the static)

Upvotes: 2

maxammann
maxammann

Reputation: 1048

Afaik enums are converted to classes and yes the values are static fields in this class: http://theopentutorials.com/tutorials/java/enum/enum-converted-to-class/

Upvotes: 1

Related Questions