I have seen this which is pretty nice solution if i had a string instead of integer, but in case all i have is the specific enum's class object and an integer, how to do i get the specific enum constant instance?
2 Answers
seem to have found the answer :
((Class<? extends Enum>)clazz).getEnumConstants()[index]
although for any-one looking for that, you should consider following @Daniel Pryden answer as most likely that using this in most use cases i can think of is bad practice.

- 8,354
- 13
- 55
- 103
Relying on the ordinal value of Java enum constants is poor practice -- it's too easy to accidentally reorder them, which would then break your code. The better solution is to simply provide your own integer that you can use instead:
public enum MyThing {
FOO(1),
BAR(2),
BAZ(3);
private final int thingId;
private MyThing(int thingId) {
this.thingId = thingId;
}
public int getThingId() {
return thingId;
}
}
Then whenever you want to get the thingId
from a MyThing
, just call the getThingId()
method:
void doSomething(MyThing thing) {
System.out.printf("Got MyThing object %s with ID %d\n",
thing.name(), thing.getThingId());
}
If you want to be able to look up a MyThing
by its thingId
, you can build a lookup table yourself and store it in a static final
field:
private static final Map<Integer, MyThing> LOOKUP
= createLookupMap();
private static Map<Integer, MyThing> createLookupMap() {
Map<Integer, MyThing> lookupMap = new HashMap<>();
for (MyThing thing : MyThing.values()) {
lookupMap.put(thing.getThingId(), thing);
}
return Collections.unmodifiableMap(lookupMap);
}
public static MyThing getThingById(int thingId) {
MyThing result = LOOKUP.get(thingId);
if (result == null) {
throw new IllegalArgumentException(
"This is not a valid thingId: " + thingId);
}
return result;
}
If you end up having a lot of enum classes and you want to do a similar thing with each of them, you can define an interface for that:
public interface Identifiable {
int getId();
}
And then make your enum implement that interface:
public enum MyThing implements Identifiable {
...
@Override
public int getId() {
return thingId;
}
}
And then you could build a reusable mechanism for looking up an Identifiable
object based on its ID.

- 59,486
- 16
- 97
- 135
-
Note that i do not know ahead of time of which type is my enum, and it might not even be an enum i myself wrote, I only have his class type at runtime and i must be able to deal with it so your answer is irrelvant sorry... thanks anyway. – Ofek Ron Nov 02 '15 at 05:23
-
@OfekRon: That's an unusual requirement. What exactly are you trying to do? Also: be aware that this answer is not just for you, it's also for future people who are searching the internet and find your question. Even if this answer is "irrelevant" to you it may be relevant to someone else. – Daniel Pryden Nov 02 '15 at 05:25
-
dont take it the wrong way, i did not down vote it because its a good answer, but its not the right answer for me hence it will not be marked as correct. And i am writing a database helper using reflections, so i can not assume all enums i get are written by me or are implementing my interfaces. – Ofek Ron Nov 02 '15 at 05:30
-
@OfekRon: You still haven't answered my question: *why* are you doing this? (Meta-question: does the world really need *yet another* Java object-relational mapping system? But I digress.) If you're trying to store the value of an arbitrary enum type, and there is no well-defined key that you can use, **the ordinal value is the wrong thing to store**. Instead you **should** be storing the string name of the enum -- that's the identifier that's guaranteed by the language to be stable. – Daniel Pryden Nov 02 '15 at 05:32
-
the String value can be changed too, its not safe either, in my opinion it is more rational that the string value would change and not the order of the enums, I myself always add enums to the end, but that doesnt matter, i am not trying to solve every possible exterme case, and the world maybe does not need it, i was doing it for my own use and for my own learning and experience. – Ofek Ron Nov 02 '15 at 05:38
-
@OfekRon: In the presence of arbitrary changes to code, nothing is guaranteed, that is sure. But the difference is that if you change the name of an enum value, you will be able to quickly detect the mismatch -- you might not be able to find the right enum value, but you will fail fast with an error. Conversely, by using the ordinal value, when the code changes underneath you, your code will **silently do the wrong thing instead of throwing an exception** which is substantially worse. – Daniel Pryden Nov 02 '15 at 05:41
-
I agree that your approach is better practice and more bug resistent. I give you that and an upvote. – Ofek Ron Nov 02 '15 at 06:47