Drive has 700+ articles for digital transformation leaders written by StarCIO Digital Trailblazer, Isaac Sacolick. Learn more.

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.

Published on:

Topics:

Leave a Reply


StarCIO

My company, StarCIO, provides leadership, learning, and advisory programs for companies looking to accelerate delivering business value from digital transformation. Contact me if you’d like to learn more about partnering opportunities.


Isaac Sacolick

Join us for a future session of Coffee with Digital Trailblazers, where we discuss topics for aspiring transformation leaders. If you enjoy my thought leadership, please sign up for the Driving Digital Newsletter and read all about my transformation stories in Digital Trailblazer.


Coffee with Digital Trailblazers hosted by Isaac Sacolick

Digital Trailblazers! Join us Fridays at 11am ET for a live audio discussion on digital transformation topics:  innovation, product management, agile, DevOps, data governance, and more!


Join the Community of StarCIO Digital Trailblazers

9 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.

  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

  3. When NOT to use apostrophes?

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

    Enums, not enum’s.

    just a thought…

  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.

  5. Anonymous

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

    http://feboook.blogspot.com

  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.

  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

  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

  9. Amazing

About Drive

Drive Agility, Innovation, Transformation

Drive is the blog for digital transformation leaders brought to you by StarCIO and Isaac Sacolick.

Agility, Innovation, and Transformation are the three primary digital transformation core competencies that every StarCIO Digital Trailblazer must champion in their organizations. Learn more About Drive.


About the StarCIO Digital Trailblazer Community

StarCIO Digital Trailblazer Community

Revolutionizing traditional learning, networking, and advising experiences.

Visit the community


About StarCIO

StarCIO

About Isaac Sacolick

Isaac Sacolick

Author, 1,000+ articles, keynote speaker, Chief StarCIO Digital Trailblazer. Full bio


Driving Digital Newsletter

Driving Digital Newsletter

StarCIO Guides

StarCIO Agile Planning Guides

Digital Trailblazer

Digital Trailblazer by Isaac Sacolick

Driving Digital

Driving Digital by Isaac Sacolick

Driving Digital Standup

Driving Digital Standup

Coffee with Digital Trailblazers

StarCIO Coffee With Digital Trailblazers

Recognition

InfoWorld 2025 Judge
InfoWorld Technology of the Year 2024 Judge
Thinkers360 Top 10 in IT Leadership
Thinkers360 Top Agile Thought Leader
Thinkers360 Top DevOps Leader
Thinkers360 Top in Digital Transfomation
Thinkers360 Top in Analytics
Thinkers360 Top in Product Management

Discover more from StarCIO Digital Trailblazer Community

Subscribe now to keep reading and get access to the full archive.

Continue reading