Alternative to enum in Java 1.4
enums, java, java1.4
Solution
Best to use in pre 1.5 is the Typesafe Enum Pattern best described in the book Effective Java by Josh Bloch. However it has some limitations, especially when you are dealing with different classloaders, serialization and so on.
You can also have a look at the Apache Commons Lang project and espacially the enum class, like John has written. It is an implementation of this pattern and supports building your own enums.
Problem
Since Java 1.4 doesn't have enums I'm am doing something like this: ``` public class SomeClass { public static int SOME_VALUE_1 = 0; public static int SOME_VALUE_2 = 1; public static int SOME_VALUE_3 = 2; public void receiveSomeValue(int someValue) { // do something } } ``` The caller of receiveSomeValue should pass one those 3 values but he can pass any other int. If it were an enum the caller could only pass one valid value. Should receiveSomeValue throw an InvalidValueException? What are good alternatives to Java 5 enums?