The Design of Software (CLOSED)

A public forum for discussing the design of software, from the user interface to the code architecture. Now closed.

The "Design of Software" discussion group has been merged with the main Joel on Software discussion group.

The archives will remain online indefinitely.

Java Enum

Hey, all -

Does anyone actually use Java enums? It seems like situations where an enum would be useful (for example, playing card suits) could easily be handled in a more lightweight fashion than the enum spec describes. However, I'm curious if anyone out there makes good use of that feature, and if so, how would you convince me to give them a shot outside of toy programs?

BrotherBeal Send private email
Thursday, April 17, 2008
I've found that they should be used in many places where you would otherwise use a boolean or (C-style) flag.


public void writeFile(boolean append)

...could be:

public static enum WriteType {REWRITE, APPEND}

public void writeFile(WriteType writeType)

...this way, calls to the method are self-documenting:


...rather than obscure:

writeFile(false); additional, most situations where booleans are used are not actually true/false situations. An enum can grow:

my name is here
Thursday, April 17, 2008
There are a lot of uses for them. Remember, these are objects too, and can have methods. You can iterate over them, and use them in switch statements.

One application I like them for is to create a finite state machine. Create an enum as a private static inner class, where each element of the enum correlates to a state. Make the instance variables of your object be the state variables, and for each "event" your state machine processes, create a method on the main object AND a method for all the enumeration elements. Store an enum as the currentState, and then delegate all event calls to the currentState:

public Foo doSomething(Bar b) { return this.currentState.doSomething(b); }

Now, the elements of the enumeration represent your state, including the logic for setting the currentState variable.

I have a bunch of other uses for them too, but hopefully that example will get you started. These are full blown objects, so there's lots of creative ways to make "OO" solutions with them.
Matt Estes Send private email
Thursday, April 17, 2008
Out of curiosity when you switch on an enum, is there a performance increase over if/else statements, as there would be when switching on an integer?
Graham Allan
Thursday, April 17, 2008
I'm pretty sure that enums are compiled down to integers for switch statements, by their ordinality.

The first place to start using them is for any "public static final" variables that are simply controls. Such as compression type: NONE, ZIP, GZIP, ...  These are usually fairly common and either implemented as an integer or string, but leaks the value. If you have anyone using the older typesafe enum trick, its a quick migration too.
Benjamin Manes Send private email
Thursday, April 17, 2008
Enums are indeed lightweight. The Java compiler handles them very intelligently. IIRC, an enum with only a few possible values is implemented as bit flags.

The advantages over using int constants (public static final int) or String constants are huge. Type-safe. Only explicitly defined values can be used.

Like other Java 5 enhancements, once you get used to enums, you'll find yourself using them all the time. Then when you need to work with Java 1.4 again you'll feel hamstrung.
Steve McLeod Send private email
Friday, April 18, 2008
We use Java enums heavily. They are great. I highly recommend.
cbmc64 Send private email
Tuesday, April 22, 2008
I have a question for one of the folks who uses enums heavily.  How do you express relations among enum classes?  I'm writing a parser, starting with a BNF grammar.  I have an enumeration of terminal symbols.  I have an enumeration of non-terminal symbols.  How do I express "grammar symbols", that is, the uninion of the terminals and the non-terminals?

I have set this up as an enumeration of all symbols, with a hokey boolean attribute specifying which are terminals, but this feels wrong.  Any help available?  Thanks,

Will Dowling
William Dowling Send private email
Friday, April 25, 2008
It sounds like you're wanting to write a parser generator...

Personally, I wouldn't use enum's to express the high level notion of the grammar directly in code(google for "parsing combinators" if you're interested in that).

But... if I were to write a parser generator, in the stage where I convert the resulting automata to Java, I would strong consider using enum's as a "low level artifact" to encapsulate the state logic and representing labels for states(probably just named "_1", "_2", ..., "_n" :) ).

If I have misunderstood your question or you want to discuss this more, feel free to click the envelope button to send me a private email...
Matt Estes Send private email
Friday, April 25, 2008
Regarding the original question (from BrotherBeal), there quite a few built-in features provided by the enum facility that would be kind of tricky (not to mention boring) to implement from scratch...

Serialization. Each enum instance reference will be preserved during serialization & deserialization (i.e., only one actual object will be created and used for each enum value).

Annotations. Enums are one of the few types allowed to be used as annotations attributes. Among other benefits, this helps improve the type-safeness of annotation heavy code.

EnumSets. The class java.util.EnumSet provides a specialized Set implementation for use with enum types. It's highly optimized and, more importantly, enables some very cool/safe idioms for handling a group of enums.
Paulo Mattos Send private email
Thursday, May 01, 2008

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics
Powered by FogBugz