Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Understanding Enum

Reply
Thread Tools

Understanding Enum

 
 
Roedy Green
Guest
Posts: n/a
 
      07-01-2005
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/...s_rumsfeld.htm

Canadian Mind Products, Roedy Green.
See http://mindprod.com/iraq.html photos of Bush's war crimes
 
Reply With Quote
 
 
 
 
Stefan Schulz
Guest
Posts: n/a
 
      07-01-2005
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"


 
Reply With Quote
 
 
 
 
Roedy Green
Guest
Posts: n/a
 
      07-01-2005
On Fri, 01 Jul 2005 16:12:14 +0200, Stefan Schulz <(E-Mail Removed)>
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/...s_rumsfeld.htm

Canadian Mind Products, Roedy Green.
See http://mindprod.com/iraq.html photos of Bush's war crimes
 
Reply With Quote
 
Stefan Schulz
Guest
Posts: n/a
 
      07-01-2005
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"


 
Reply With Quote
 
Joan
Guest
Posts: n/a
 
      07-01-2005

"Stefan Schulz" <(E-Mail Removed)> wrote in message
news(E-Mail Removed).. .
> 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"
>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
ENUM server for private ENUM kael UK VOIP 2 02-25-2007 11:54 AM
enum: display elements of an enum specified at runtime Jerminia Java 3 10-07-2005 10:08 PM
enum within an enum - Java 6 06-13-2005 12:51 AM
Including an enum within another enum, possible? mrhicks C Programming 2 06-10-2004 03:00 AM
How to enum an enum? Ernst Murnleitner C++ 5 11-13-2003 11:06 AM



Advertisments