Understanding Enum

R

Roedy Green

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
 
S

Stefan Schulz

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
 
R

Roedy Green

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
 
S

Stefan Schulz

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.
 
J

Joan

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top