Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Weird enum

 
Ade Barkah
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bug or feature?
Anyway, the example reminds me to anonymous classes with new methods that can never be invoked, as



Bu.
 
Ade Barkah
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic