File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Weird enum Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Weird enum" Watch "Weird enum" New topic

Weird enum

Ade Barkah
Ranch Hand

Joined: Mar 17, 2004
Posts: 65
I'm surprised a constant specific class body may define a method not matching anything in the enum body itself:

Bug or feature?
[ October 08, 2006: Message edited by: Ade Barkah ]

-Ade<br /><a href="" target="_blank" rel="nofollow"></a>
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
Bug or feature?
Anyway, the example reminds me to anonymous classes with new methods that can never be invoked, as


all events occur in real time
Ade Barkah
Ranch Hand

Joined: Mar 17, 2004
Posts: 65
Well, technically in either case the methods can be invoked via reflection.

(Enum version analogous).

The problem with the enum case is typically we want to create a special version of a method declared in the enum body:

Notice GREEN's version has incorrect spelling, and the wrong method will be called for GREEN without any warning.
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2398
I don't think this is a deficiency of the enum concept. An enum is just basically a special instance of a class.

The example you cited could easily happen when a programmer accidentally misspells an inherited method when they mean to override it.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
We're beyond the scope of SCJP certification here. To discuss the merits or disavantage of enum features and using reflection please use one of our Java In General (Intermediate) or (Advanced) forums. Moving to JIG(Intermediate)...

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
I agree. Here's the link:
subject: Weird enum
It's not a secret anymore!