Understanding Enum

Discussion in 'Java' started by Roedy Green, Jul 1, 2005.

  1. Roedy Green

    Roedy Green Guest

    I have been experimenting rewriting code using enums to see how they
    differ from the old int style and class style kludged enums.

    Enums have a number of rough edges.

    1. they are not inner classes, at least I have not yet figured out how
    to make them work that way. This means they cannot communicate as
    easily with the enclosing class.

    2. the syntax of commas and {} gets pretty tenuous when you start
    adding custom methods for enum constants and methods for the enum as a
    whole. The enum constant names can get lost in the syntactic noise.

    3. there are magic pre-constructed methods you cannot override,
    equals, valueOf, compareTo.

    4. I'm not sure if enum constant methods can override enum as a whole
    methods.

    5. Enum constructors can't touch static data. Ouch!

    oddly, it seems much harder to convert code that was done with enum
    classes than enum ints.




    --
    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 1, 2005
    #1
    1. Advertising

  2. On Fri, 01 Jul 2005 09:43:30 +0000, Roedy Green wrote:

    > I have been experimenting rewriting code using enums to see how they
    > differ from the old int style and class style kludged enums.
    >
    > Enums have a number of rough edges.
    >
    > 1. they are not inner classes, at least I have not yet figured out how
    > to make them work that way. This means they cannot communicate as
    > easily with the enclosing class.


    Erm, if i understood you correctly, it is a matter of your code layout.
    inner enums work just fine, as long as they are members of either a
    top-level or a static inner class. However, the class needs to be static.

    > 2. the syntax of commas and {} gets pretty tenuous when you start
    > adding custom methods for enum constants and methods for the enum as a
    > whole. The enum constant names can get lost in the syntactic noise.


    Erm, i am not sure i follow here. What is the problem with

    enum Foo {
    BAR,
    BAZ;

    public void fooify(Foo f){
    nonsense++;
    /* ... */
    }
    }

    > 3. there are magic pre-constructed methods you cannot override,
    > equals, valueOf, compareTo.


    equals is not magic, it merely is defined final in Enum. (If this was
    necessary is open to debate, but i believe it is a good choice)

    > 4. I'm not sure if enum constant methods can override enum as a whole
    > methods.


    Okay, now you officially lost me.

    > 5. Enum constructors can't touch static data. Ouch!


    Okay, this one is a limitation i do not quite follow. But then again, why
    bother with the constructors at all? All instances of your enum are known
    at compile time, so you might just hard-wire that, or put it in a static
    initializer

    > oddly, it seems much harder to convert code that was done with enum
    > classes than enum ints.


    --
    You can't run away forever,
    But there's nothing wrong with getting a good head start.
    --- Jim Steinman, "Rock and Roll Dreams Come Through"
     
    Stefan Schulz, Jul 1, 2005
    #2
    1. Advertising

  3. Roedy Green

    Roedy Green Guest

    On Fri, 01 Jul 2005 16:12:14 +0200, Stefan Schulz <>
    wrote or quoted :

    >> 4. I'm not sure if enum constant methods can override enum as a whole
    >> methods.

    >
    >Okay, now you officially lost me.




    I am typing off the top of my head, so the punctuation will likely be
    wrong.

    enum { roedy{
    // roedy's version of the getSex method
    void getSex() { super.getSex(); }
    }

    brenda, alvin }

    // method common to all enum constants, possibly overriden or only
    used as a default.

    void getSex() { doSomething(); }

    }


    My question is, which can be answered soon by experiment, does
    super.getSex() have meaning in the roedy enum constant. The more I
    think about it the more it has to be so.

    I think I need to see some enum examples written out longhand as the
    equivalent class files.

    --
    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 1, 2005
    #3
  4. On Fri, 01 Jul 2005 15:26:34 +0000, Roedy Green wrote:

    > I am typing off the top of my head, so the punctuation will likely be
    > wrong.
    >
    > enum { roedy{
    > // roedy's version of the getSex method
    > void getSex() { super.getSex(); }
    > }
    >
    > brenda, alvin }
    >
    > // method common to all enum constants, possibly overriden or only
    > used as a default.
    >
    > void getSex() { doSomething(); }
    >
    > }
    >
    >
    > My question is, which can be answered soon by experiment, does
    > super.getSex() have meaning in the roedy enum constant. The more I
    > think about it the more it has to be so.


    Uhm, why this horribly contrived style at all? Why not just include this
    in the normal getSex() method? I mean, you actually declare a different
    subclass of your enum, just to implement a behaviour that could easily and
    with little loss of functionality be included in the normal class.

    --
    You can't run away forever,
    But there's nothing wrong with getting a good head start.
    --- Jim Steinman, "Rock and Roll Dreams Come Through"
     
    Stefan Schulz, Jul 1, 2005
    #4
  5. Roedy Green

    Joan Guest

    "Stefan Schulz" <> wrote in message
    news:p...
    > On Fri, 01 Jul 2005 09:43:30 +0000, Roedy Green wrote:
    >
    > > I have been experimenting rewriting code using enums to see how they
    > > differ from the old int style and class style kludged enums.


    Similarly, I have been examining the banana peel for possible use
    in the automotive industry; but they seem to have rough edges as well.
    In fact, once we get away from encapsulating bananas, which is
    what they were designed to do, they are pretty useless. For example,
    as a window crank they don't work at all.

    > >
    > > Enums have a number of rough edges.
    > >
    > > 1. they are not inner classes, at least I have not yet figured out how
    > > to make them work that way. This means they cannot communicate as
    > > easily with the enclosing class.

    >
    > Erm, if i understood you correctly, it is a matter of your code layout.
    > inner enums work just fine, as long as they are members of either a
    > top-level or a static inner class. However, the class needs to be static.
    >
    > > 2. the syntax of commas and {} gets pretty tenuous when you start
    > > adding custom methods for enum constants and methods for the enum as a
    > > whole. The enum constant names can get lost in the syntactic noise.

    >
    > Erm, i am not sure i follow here. What is the problem with
    >
    > enum Foo {
    > BAR,
    > BAZ;
    >
    > public void fooify(Foo f){
    > nonsense++;
    > /* ... */
    > }
    > }
    >
    > > 3. there are magic pre-constructed methods you cannot override,
    > > equals, valueOf, compareTo.

    >
    > equals is not magic, it merely is defined final in Enum. (If this was
    > necessary is open to debate, but i believe it is a good choice)
    >
    > > 4. I'm not sure if enum constant methods can override enum as a whole
    > > methods.

    >
    > Okay, now you officially lost me.
    >
    > > 5. Enum constructors can't touch static data. Ouch!

    >
    > Okay, this one is a limitation i do not quite follow. But then again, why
    > bother with the constructors at all? All instances of your enum are known
    > at compile time, so you might just hard-wire that, or put it in a static
    > initializer
    >
    > > oddly, it seems much harder to convert code that was done with enum
    > > classes than enum ints.

    >
    > --
    > You can't run away forever,
    > But there's nothing wrong with getting a good head start.
    > --- Jim Steinman, "Rock and Roll Dreams Come Through"
    >
    >
     
    Joan, Jul 1, 2005
    #5
    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. -

    enum within an enum

    -, Jun 12, 2005, in forum: Java
    Replies:
    6
    Views:
    591
  2. Jerminia
    Replies:
    3
    Views:
    659
    Roedy Green
    Oct 7, 2005
  3. Ernst Murnleitner

    How to enum an enum?

    Ernst Murnleitner, Nov 12, 2003, in forum: C++
    Replies:
    5
    Views:
    507
    Rolf Magnus
    Nov 13, 2003
  4. KevinSimonson

    Understanding an Enum Class Declaration

    KevinSimonson, Oct 1, 2010, in forum: Java
    Replies:
    6
    Views:
    599
    Roedy Green
    Oct 2, 2010
  5. arnuld

    understanding enum type

    arnuld, Nov 29, 2010, in forum: C Programming
    Replies:
    15
    Views:
    721
    Eric Sosman
    Dec 7, 2010
Loading...

Share This Page