• 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 static methods

 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
quick question concerning enums: if enums can have static methods inside them, then are enums static by definition?

thanks,
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Enums are implicitly static.
 
J Brewer
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Maybe I should clarify:



the answer is: The code will fail to compile because the abstract method 'performance' needs to be implemented by all the enum constants, i.e., AVERAGE, GOOD and EXCELLENT.

so do i understand correctly that enums are static by definition, just not coded in the declaration line?
 
J Brewer
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Keith. One more question: is it always the case that if you have an abstract method in the enum, that all constants must implement that abstract method?
 
Keith Lynn
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, that is the case.

Take a look at this article.
 
J Brewer
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, I appreciate it.
 
Ranch Hand
Posts: 809
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


How a enum declared at top level would be static? It would be static if defined in some class as a member.

Above code will give compilation error because enums are final. When you have abstract method in it, then enum class must be abstract as well. Abstract and final are illegal combinations.

Regards

Naseem
[ July 10, 2006: Message edited by: Naseem Khan ]
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do you not mean something like this?


Which can be used like this?
 
J Brewer
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I guess what I was asking is whether enum's are implicitly static, without being declared so? And even when the enum is declared outside a class? I know that you cannot instantiate an enum, so that's why I was asking.
 
Keith Lynn
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm sorry. I should have said that a nested enum is implicitly static.

This is from the Java Language Specification 8.9

An enum type is implicitly final unless it contains at least one enum constant that has a class body. In any case, it is a compile-time error to explicitly declare an enum type to be final.

Nested enum types are implicitly static. It is permissable to explicitly declare a nested enum type to be static.
reply
    Bookmark Topic Watch Topic
  • New Topic