Java Practice: When NOT to use enum's
We said goodbye to public final static int's when we upgraded to Java5 some time ago. In it's place, we are using Java's enum type. There are some really good tutorials on using Java enum's (see JavaWorld's tutorial for example), but here are some of the selling points:- type safety
- code completion in Eclipse
- self documenting
- valueOf method makes it easy to convert from strings to enum values
- enums can have internal methods
- easy to use in collections
- allows you to store additional attributes with each enumeration.
Java's enum's are very much like classes. In fact, they are special classes that are inherited from the java.lang.Enum class. Therein lies the first limitation:
enum's can't extend other enum's since they all extend java.lang.Enum
So you can't create an enum FourSided that extends Shape.
Another peculiarity of enum's is that they are effectively singleton objects instantiated when they are first used. If you're not careful with your design and use of enums (e.g., enum abuse), this can get you in trouble. Specifically, if you use an enum like a full blown class. Here was the scenario (somewhat simplified)
Read full article from Java Practice: When NOT to use enum's
No comments:
Post a Comment