This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

enum static methods

 
J Brewer
Ranch Hand
Posts: 46
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
quick question concerning enums: if enums can have static methods inside them, then are enums static by definition?

thanks,
 
Keith Lynn
Ranch Hand
Posts: 2399
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Enums are implicitly static.
 
J Brewer
Ranch Hand
Posts: 46
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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: 2399
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, that is the case.

Take a look at this article.
 
J Brewer
Ranch Hand
Posts: 46
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, I appreciate it.
 
Naseem Khan
Ranch Hand
Posts: 809
  • 0
  • Mark post as helpful
  • send pies
  • 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 ]
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you not mean something like this?


Which can be used like this?
 
J Brewer
Ranch Hand
Posts: 46
  • 0
  • Mark post as helpful
  • send pies
  • 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: 2399
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic