Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / December 2005

Tip: Looking for answers? Try searching our database.

A bafflement moment on enum

Thread view: 
Roedy Green - 22 Dec 2005 13:19 GMT
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.
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Ingo R. Homann - 22 Dec 2005 13:31 GMT
Hi Roedy,

> 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
[quoted text clipped - 19 lines]
> 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
Roedy Green - 22 Dec 2005 14:49 GMT
On Thu, 22 Dec 2005 14:31:20 +0100, "Ingo R. Homann"
<ihomann_spam@web.de> wrote, quoted or indirectly quoted someone who
said :

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

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Chris Smith - 22 Dec 2005 15:52 GMT
> 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.

Signature

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

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation

Ingo R. Homann - 23 Dec 2005 11:19 GMT
Hi,

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

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
Chris Smith - 23 Dec 2005 14:40 GMT
> 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."

Signature

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

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.