Women in Technology

Hear us Roar



Article:
  Enums in Java (One More Time)
Subject:   switch or 'enums as objects'
Date:   2003-04-28 04:34:15
From:   pverkest
I like the enums-as-objects, because it forces me to use objects and to think OO. It happened quite often that I introduced an enum-as-object and in a later phaze, added behavior to the enum.
What's called 'Seamless Integration with Other Language Features' in the article seems to be not OO at all: in stead of using polymorphism and implementing a method on all days, a switch is preferred.
----
Peter Verkest
Main Topics Oldest First

Showing messages 1 through 1 of 1.

  • switch or 'enums as objects'
    2003-04-28 15:08:35  jimoore [View]

    > I like the enums-as-objects, because it forces me to use
    > objects and to think OO. It happened quite often that I
    > introduced an enum-as-object and in a later phaze, added
    > behavior to the enum.

    I am in complete agreement. I wrote the jEnum translator to simplify the use of the enums-as-objects approach, and then I subsequently wrote this article to make it available to others.


    > What's called 'Seamless Integration with Other Language
    > Features' in the article seems to be not OO at all: in stead
    > of using polymorphism and implementing a method on all days,
    > a switch is preferred.

    This was not my intent at all. In general, one should prefer polymorphism over a switch statement, and in the article I suggested that the reason enums were omitted was to encourage polymorphism and discourage switch statements. In my C++ training course developed in the early 1990's -- more than a decade ago -- I used several slides to discuss exactly this topic and to emphasize that the use of polymorphism was the preferred approach. I also stated that programmers should not get carried away trying to eliminate all switch statement. Some uses are still valid.

    However, one does not have to look far to find examples that use the enums-as-integer constants approach. The reference to the paper by James Gosling (the original creator of Java) and Henry McGilton cited in the Resources section contains an example of the enums-as-integer-constants approach. Moreover, the following standard Java classes represent only a small subset of those that use enums-as-integer-constants together with switch statements:

    java.util.GregorianCalendar
    java.awt.WindowEvent
    java.awt.FlowLayout
    java.awt.GridBagLayout
    java.awt.TextArea
    javax.print.MimeType
    javax.swing.SwingConstants/SwingUtilities
    javax.swing.JMenuItem/java.awt.MouseEvent

    Also note that JSR 201, the current proposal to add enums to J2SE Release 1.5 ("Tiger"), contains a modification to the language to allow them to be used in switch statements (see http://jcp.org/aboutJava/communityprocess/jsr/tiger/enum.html). In general I do not support language changes for Java since I want the language itself to remain as simple as possible, but if one is going to add enums to Java, it only makes sense to allow them to be used in switch statements.

    John I. Moore, Jr.