A bafflement moment on enum

R

Roedy Green

I have written enum code that works, but suddenly I had a sinking
feeling that it has no business working. I did a little disassembling
and sorted out the mystery.

let's say I create an enum with method next().

Then I override the next() method with custom code in the various
enum constants.

These methods live in anonymous inner classes of the enum (though
oddly they decompile as static).

Then I do something like this:

Breed d = Breed.DALMATIAN;
d.next();

How on earth does Java know to use DALMATIAN.next() rather than
Breed.next()?

The answer is that d contains a reference to the DALMATIAN inner class
that EXTENDS the Breed enum class as well as being an inner class of
it. So the next() method overrides the one in the Breed class.
 
I

Ingo R. Homann

Hi Roedy,

Roedy said:
I have written enum code that works, but suddenly I had a sinking
feeling that it has no business working. I did a little disassembling
and sorted out the mystery.

let's say I create an enum with method next().

Then I override the next() method with custom code in the various
enum constants.

These methods live in anonymous inner classes of the enum (though
oddly they decompile as static).

Then I do something like this:

Breed d = Breed.DALMATIAN;
d.next();

How on earth does Java know to use DALMATIAN.next() rather than
Breed.next()?

The answer is that d contains a reference to the DALMATIAN inner class
that EXTENDS the Breed enum class as well as being an inner class of
it. So the next() method overrides the one in the Breed class.

If I understand you correctly, this is the "normal" behaviour that has
nothing to do with enums but worked the same in Java 1.2 where there
were no enums:

class Breed
{

class DALMATIAN extends Breed
{

public void next()
{
System.out.println("DALMATIAN.next()");
}
}

public void next()
{
System.out.println("Breed.next()");
}
}

So, what is the problem about this?

Ciao,
Ingo
 
R

Roedy Green

So, what is the problem about this?

The problem was not realising that the inner classes for each enum
class EXTENDED the enum main class. If they didn't, how enums worked
would be quite mysterious.
 
C

Chris Smith

Roedy Green said:
The problem was not realising that the inner classes for each enum
class EXTENDED the enum main class. If they didn't, how enums worked
would be quite mysterious.

This is, in any case, the obvious implementation. I'd be careful about
making an assertion about the LANGUAGE that this happens. It is
certainly true in the Sun reference implementation, and will probably be
true of any other implementation you happen to be working with, as well.

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

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
I

Ingo R. Homann

Hi,

Chris said:
This is, in any case, the obvious implementation. I'd be careful about
making an assertion about the LANGUAGE that this happens....

IIRC, this behaviour is specified in the langspec.

I do not remember if the kind of implementation as *sub*class is
specified as well - that means i do not remember if
"Breed.DALMATIAN.getClass() != Breed.getClass()" is true - which should
be true if it is a *sub*class. But of course "Breed.DALMATIAN instanceof
Breed" must be true.

But independant of the welldefined behaviour: I cannot see any reason
(performance?) to implement enums not as subclasses...

Ciao,
Ingo
 
C

Chris Smith

Ingo R. Homann said:
IIRC, this behaviour is specified in the langspec.

Yes, you're right. I should start reading the JLS more. Section 8.9
says:

"The optional class body of an enum constant implicitly defines an
anonymous class declaration (15.9.5) that extends the immediately
enclosing enum type."

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

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 

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

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top