Basically, the compiler (javac) translates your enum into a static array containing all of your values at compile time. When you call values(), it gives you a . Clone'd() copy of this array Given this simple enum: public enum Stuff { COW, POTATO, MOUSE; } You can actually look at the code that Java generates: public enum Stuff extends Enum { /*public static final*/ COW /* = new Stuff("COW", 0) */, /*public static final*/ POTATO /* = new Stuff("POTATO", 1) */, /*public static final*/ MOUSE /* = new Stuff("MOUSE", 2) */; /*synthetic*/ private static final Stuff $VALUES = new Stuff{Stuff.
COW, Stuff. POTATO, Stuff. MOUSE}; public static Stuff values() { return (Stuff)$VALUES.clone(); } public static Stuff valueOf(String name) { return (Stuff)Enum.
ValueOf(Stuff. Class, name); } private Stuff(/*synthetic*/ String $enum$name, /*synthetic*/ int $enum$ordinal) { super($enum$name, $enum$ordinal); } } You can look at how javac 'translates' your classes by making a temporary directory and running: javac -d -XD-printflat filename.java.
Basically, the compiler (javac) translates your enum into a static array containing all of your values at compile time. When you call values(), it gives you a . Clone'd() copy of this array.
Given this simple enum: public enum Stuff { COW, POTATO, MOUSE; } You can actually look at the code that Java generates: public enum Stuff extends Enum { /*public static final*/ COW /* = new Stuff("COW", 0) */, /*public static final*/ POTATO /* = new Stuff("POTATO", 1) */, /*public static final*/ MOUSE /* = new Stuff("MOUSE", 2) */; /*synthetic*/ private static final Stuff $VALUES = new Stuff{Stuff. COW, Stuff. POTATO, Stuff.
MOUSE}; public static Stuff values() { return (Stuff)$VALUES.clone(); } public static Stuff valueOf(String name) { return (Stuff)Enum. ValueOf(Stuff. Class, name); } private Stuff(/*synthetic*/ String $enum$name, /*synthetic*/ int $enum$ordinal) { super($enum$name, $enum$ordinal); } } You can look at how javac 'translates' your classes by making a temporary directory and running: javac -d -XD-printflat filename.java.
1 for neatness factor. – nalroff Apr 27 at 17:09 Wouldn't a System. Arraycopy be faster?
– Gandalf Aug 3 at 18:43.
If you assign it to a local variable the only thing that you can modify is assigning another enum to this variable. This will not change the enum itself because you are only changing the object your variable references. It seems that the enums are in fact singletons so that only one element from each enum can exist in you whole program this makes the == operator legal for enums.So there is no performance problem and you can't accidentally change something in your enum definition.
1 I think the OP means modifying the array returned by values(). If this is the same array object that is kept internally (instead of a copy), then modifying it (e.g. Assigning one element to another, assigning null to an element, etc.) would mess it up, not only for the enum class, but for any future calls to values() – newacct Jul 22 '09 at 5:58 Yes you are right. If there wasn't a clone you could remove a Enum from the array and later calls from values would miss this value.
– Janusz Jul 22 '09 at 14:43.
This question concerns the "values()" method. Specifically, how is it implemented? Usually, I could jump to the source for Java classes using F3 or CTRL+Click in Eclipse (even for classes like String, Character, Integer, and even Enum).
It is possible to view the source of the other enum methods (e.g. Does "values()" create a new array each time it is invoked? If I assign it to a local variable and then modify one of the elements, what happens (clearly this won't affect the value returned by values(), which implies that a new array is allocated each time). Is the code for it native?
Or does the JVM / compiler treat it specially, only returning a new instance from values() when it cannot prove that it will not be modified. Thanks in advance.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.