I’m trying to declare an enum type based on data that I’m retrieving from a database. I have a method that returns a string array of all the rows in the table that I want to make into an enumerated type. Is there any way to construct an enum with an array?
This is what I tried, but from the way it looked in eclipse, it seemed like this just created a method by that name:
public enum ConditionCodes{
Condition.getDescriptions();
}
Thank you in advance!
You can’t.
The values of an
enummust be known at compile time. If you have anything else, then it’s not anenum.You could come rather close via an implementation that’s similar to the old typesafe enums that were used before the Java language introduced support for this technique via the
enumkeyword. You could use those techniques but simply replace thestatic finalfields with values read from the DB.