I'm making a program that has an enum that us used for a list of types but I want it to have the possibility of being expanded later at runtime and am not sure what is the best way to do this is. The only ways I have been able to think of are using arrays of classes in place of the enum which I don't want to do as I would loose the simplicity of the enum.
-
1"An enum type is a special data type that enables for a variable to be a set of predefined constants." It doesn't really sound like an enum anymore here :) – Zavior Sep 28 '13 at 12:21
-
2An enum is used to enumerate constants that are fixed and already well-known at compile time. If you have data that is not already well-known to you at compile time (i.e., you don't know it all or expect it to change often) then an enum is not likely the best way to represent it. – scottb Sep 28 '13 at 12:22
-
2This is hardly a use case for an `Enum`. Use a `Set` in combination with some class of your own instead. – toniedzwiedz Sep 28 '13 at 12:23
-
Is it possible to make an enum implement an interface and how would I do that with multiple enums? – user2248702 Sep 28 '13 at 12:23
-
Yes, but different question. – scottb Sep 28 '13 at 12:24
5 Answers
Enums are supposed to be completely static enumerations, therefore you must be able to know the exact values the enum covers at compile time.
You could generate the java file at compile time allowing for a bit more flexibility over the values, although this would be rather over the top.
Your best bet would be:
1) To use a class, potentially with a set of predefined instances, so that new instance of said class can be created at runtime.
or
2) To create an interface that an enum containing the default values implements, allowing new instances to be created at runtime, whilst retaining some form of enum structure.

- 7,449
- 2
- 29
- 45
-
The extra values of the enum will only be used by the extended portion of the program. – user2248702 Sep 28 '13 at 12:27
-
Java enums cannot be expanded/extended at runtime. They are more like constants. If you need extendable enums, maybe enum
is not the right concept for you!
Consider using a simple class that you instantiate at runtime.

- 24,105
- 2
- 29
- 50
If you can't know all values that needs to be there at compile time, I'd just use strings. If you want to associate attributes or behaviors to it, you can use a map. This is fairly common in Java. Also in python, people use strings for enums.
Java 7 made switch statement work on strings, which IMO is a move to support this use of strings.
For an example of such use in Java, see this: https://github.com/netty/netty/blob/master/example/src/main/java/io/netty/example/telnet/TelnetClientInitializer.java

- 26,542
- 13
- 70
- 109
-
When using Strings, you lose some of the power of Java enums though - you can't define extra methods for String's – Sinkingpoint Sep 28 '13 at 12:26
-
1The `int` or `String` enum pattern should be considered an anti-pattern in Java. The Java `enum` language feature provides guarantees of type safety and instance control (as well as most of the great features of object extensibility) that the int/String enum pattern lacks. Moreover, code that uses constant-specific enum methods in place of `switch` statements on values is less fragile and easier to maintain. – scottb Sep 28 '13 at 12:27
-
@scottb: I agree, except when values that needs to be supported are not known at compile time. Then you can't use enums. – Enno Shioji Sep 28 '13 at 12:31
Enumerations are fixed at compile time. How exactly would you "use the simplicity"? You can't for example write a switch
command using constants that are not known in advance.
If the point is to branch based on the type of something, you can use classical OO decomposition (create methods in every type in the list and let polymorphism select the correct branch) or use a HashMap from class objects to functional objects or strategy implementations.

- 5,209
- 17
- 22
You can try ExpandableStringEnum. Ref: https://learn.microsoft.com/en-us/java/api/com.azure.core.util.expandablestringenum?view=azure-java-stable
It has equals(object) to compare and values() which returns a collection API as well using which you can enumerate. And you can instantiate a type in runtime as well.
Using fromString(String name, Class clazz) API.
// Define an expandable string enum class
public final class Color extends ExpandableStringEnum<Color> {
public static final Color RED = fromString("Red", Color.class);
public static final Color GREEN = fromString("Green", Color.class);
public static final Color BLUE = fromString("Blue", Color.class);
}
// Create an instance of an expandable string enum from a String value
Color color = Color.fromString("Red", Color.class);
// Compare two instances of an expandable string enum
if (color == Color.RED) {
// do something
}

- 111
- 5