Originally posted by Junaid Bhatra:
This pattern seems familiar. If I'm not mistaken, I think java.lang.Boolean uses something similar (it may not be the exact same pattern though).
Originally posted by JUNILU LACAR:
I got a chance to browse a couple of books last week and found some twists to the Typesafe Enum pattern:
The first twist was from "Java 2 Performance and Idiom Guide" by Craig Larman and Rhett Guthrie. They declared the typesafe enum constants in an interface. The motivation was allow a class to have direct access to the constants without having to specify the interface name by simply implementing the interface. The only problem I saw with the approach was if a class implemented two typesafe enum interfaces that happened to have the same constant names, you would still need to qualify the constants with their respective interface names. It's an interesting approach but IMO, the typing you save by not having to qualify the constant is not worth the risk of bugs creeping in because you didn't.
"Java in Practice" by Nigel Warren and Philip Bishop discuss the second twist which addresses the issue of defining a constant enum that represents null. The solution in the book involves using a proxy class and a valueOf() class method.