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.
The last point two points are where enums depart from normal static ints. For example an enum for Shape could have an attribute 'sides'. (Why do developers love using shapes...).

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.

8 comments:

  1. I think the other key case for NOT using enums is when you need an expandable (or nested) set of values. Since you can't come along and subclass the enum later, you need some other solution. If you're in a position to expand the enum yourself at a later date, that's fine. But that's not always the case.

    ReplyDelete
  2. Another 'gotcha' with enum's is that they don't get along with Expression Language (EL) in JSP's very well. I blogged about it here: Enum in EL, JSP

    ReplyDelete
  3. When NOT to use apostrophes?

    Apostrophes aren't used for plurals, they are used for possessives.

    Enums, not enum's.

    just a thought...

    ReplyDelete
  4. Thanks Joe! This post has been up since Jan 31 and you're the first to notice my mistake.

    Alex is correct - if you have properties that need to e expandable, nested, inheritable or hierarchical then that's a clue that enums (no apostrophe :-) ) are not the way to go. Still, I'm a strong believer in refactoring so maybe enums get you started, then build out classes or use another property structure later on.

    Thanks for the additional tip Zac.

    ReplyDelete
  5. you have nice site. thanks for sharing this site. various kinds of ebooks are available here

    http://feboook.blogspot.com

    ReplyDelete
  6. When I bought my computer and I didn´t know how to use java graphics, so I decided looking for information in a webside and I found an useful information that helped me a lot.. Now I am interested in to do the best investment and I found a webside very useful and interesting called costa rica investment opportunities , I think it´s a very wonderful site.

    ReplyDelete
  7. A very good and informative article indeed . It helps me a lot to enhance my knowledge, I really like the way the writer presented his views. I hope to see more informative and useful articles in future.

    Web Development Singapore

    ReplyDelete
  8. Thank Isaac Sacolick your article. it's very informative you measion some disadvantages of enum type. I also write about some disadvantages of enum type

    ReplyDelete

Share