• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Enum Methods

 
Ranch Hand
Posts: 182
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Enum constants cannot have their own methods???
 
Ranch Hand
Posts: 210
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
simplified version of the decompiled class, please note that PROGRAMMER is a static variable of EnumDesignation class.so it can call only methods of EnumDesignation


 
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Its very simple, actually. Can you do this.



Well, my code example is not that relevant. But tell me, is that method is part of EnumDesignation? No, its not. That's the reason its not visible to the outer world.
 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I guess you have to do this:

((Integer)i).doubleValue()
 
Jia Tan
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Back to the original post, I think constant specific class body in enum is used to override an existing enum method for a particular enum constant. If you define new method in the constant specific class body, it will never be reached, since there is no way to reference it. Correct me if I am wrong about this.
 
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The JLS has this to say (8.9):


The optional class body of an enum constant implicitly defines an anonymous
class declaration (ยง15.9.5) that extends the immediately enclosing enum type. The
class body is governed by the usual rules of anonymous classes; in particular it
cannot contain any constructors.


Now, there is no rule that says that you can't define additional methods (methods that are not in the interface or class that the anonymous class implements or extends) in an anonymous class. This is even though the same argument given that these methods will ultimately not be callable at runtime is true.
Why doesn't this work for enums? I'll leave that for you to decide and report back.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic