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:
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)
enum Duration {
TwoWeek(dateFunctionTodayMinus(14)), FourWeek(dateFunctionTodayMinus(28)));
Date startDate;
Duration(Date d) { startDate = d);
}
Simplified.... dateFunctionTodayMinus(int X) returns a date of today minus X days.
We then had a class that used these enums. The developer assumed that these enums would get defined at runtime so that Duration.TwoWeek would always represent a date range starting two weeks from 'now'.
Wrong....
Duration.TwoWeek is instantiated at first use. All subsequent uses of Duration.TwoWeek will reflect the dates of when it was first used. Ouch.
The lesson here is that enums are not classes/objects. Enums are good to represent static/singleton objects but should never be used as value objects or have attributes that get set during usage. You can use an enum to 'type/label' a two week duration, but the actual start/end dates should be attributes of a class DateRange.
continue reading "Java Practice: When NOT to use enum's"
- 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)
enum Duration {
TwoWeek(dateFunctionTodayMinus(14)), FourWeek(dateFunctionTodayMinus(28)));
Date startDate;
Duration(Date d) { startDate = d);
}
Simplified.... dateFunctionTodayMinus(int X) returns a date of today minus X days.
We then had a class that used these enums. The developer assumed that these enums would get defined at runtime so that Duration.TwoWeek would always represent a date range starting two weeks from 'now'.
Wrong....
Duration.TwoWeek is instantiated at first use. All subsequent uses of Duration.TwoWeek will reflect the dates of when it was first used. Ouch.
The lesson here is that enums are not classes/objects. Enums are good to represent static/singleton objects but should never be used as value objects or have attributes that get set during usage. You can use an enum to 'type/label' a two week duration, but the actual start/end dates should be attributes of a class DateRange.