When is enum type applicable?

Discussion in 'Java' started by -, Jul 24, 2005.

  1. -

    - Guest

    If enum type cannot be subclassed, using MouseWheelEvent and MouseEvent
    as an example where the former is the latter's subclass, how can the
    constants be made into enum type?


    Static constants from MouseEvent:

    static int MOUSE_CLICKED

    static int MOUSE_DRAGGED

    // etc
    // ...


    Static constants from MouseWheelEvent:

    static int WHEEL_BLOCK_SCROLL

    static int WHEEL_UNIT_SCROLL



    or is enum type not applicable in this matter? if so, when is enum
    applicable?
    -, Jul 24, 2005
    #1
    1. Advertising

  2. -

    Chris Smith Guest

    - <> wrote:
    > If enum type cannot be subclassed, using MouseWheelEvent and MouseEvent
    > as an example where the former is the latter's subclass, how can the
    > constants be made into enum type?


    ....

    > or is enum type not applicable in this matter? if so, when is enum
    > applicable?


    Enumerations are definitely not applicable in that case. There are
    several reasons for enums, but the one Java chose to focus on is type
    safety; an enum guarantees that the variable will not have any value
    except those listed. The ability to subclass and add more possible
    values would violate that guarantee, because those values would need to
    be assignment-compatible with the supertype.

    Where are enums applicable? Anywhere you have a set of possible values
    that you DO want to enforce strictly.

    --
    www.designacourse.com
    The Easiest Way To Train Anyone... Anywhere.

    Chris Smith - Lead Software Developer/Technical Trainer
    MindIQ Corporation
    Chris Smith, Jul 24, 2005
    #2
    1. Advertising

  3. -

    Roedy Green Guest

    On Sun, 24 Jul 2005 12:14:46 +0800, - <> wrote or
    quoted :

    >If enum type cannot be subclassed, using MouseWheelEvent and MouseEvent
    >as an example where the former is the latter's subclass, how can the
    >constants be made into enum type?


    Each enum constant can be its own inner anonymous class with its own
    methods and member variables.

    Just write something like this

    enum { RED( a, b, c ) { void m( x ) { ...} },

    m is a method of inner class RED which is also an enum constant.
    a, b, c are parms to its constructor.


    --
    Bush crime family lost/embezzled $3 trillion from Pentagon.
    Complicit Bush-friendly media keeps mum. Rumsfeld confesses on video.
    http://www.infowars.com/articles/us/mckinney_grills_rumsfeld.htm

    Canadian Mind Products, Roedy Green.
    See http://mindprod.com/iraq.html photos of Bush's war crimes
    Roedy Green, Jul 24, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Keith
    Replies:
    1
    Views:
    472
    Keith
    Feb 8, 2004
  2. Eric Sosman
    Replies:
    3
    Views:
    778
    Mayeul
    Feb 26, 2010
  3. Wojtek
    Replies:
    1
    Views:
    495
  4. Lew
    Replies:
    0
    Views:
    534
  5. Roedy Green
    Replies:
    0
    Views:
    728
    Roedy Green
    Feb 27, 2010
Loading...

Share This Page