File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes enum static methods 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 "enum static methods" Watch "enum static methods" New topic
Author

enum static methods

J Brewer
Ranch Hand

Joined: Jul 10, 2006
Posts: 46
quick question concerning enums: if enums can have static methods inside them, then are enums static by definition?

thanks,
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
Enums are implicitly static.
J Brewer
Ranch Hand

Joined: Jul 10, 2006
Posts: 46
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

Joined: Jul 10, 2006
Posts: 46
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

Joined: Feb 07, 2005
Posts: 2367
Yes, that is the case.

Take a look at this article.
J Brewer
Ranch Hand

Joined: Jul 10, 2006
Posts: 46
Thanks, I appreciate it.
Naseem Khan
Ranch Hand

Joined: Apr 25, 2005
Posts: 809


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 ]

Asking Smart Questions FAQ - How To Put Your Code In Code Tags
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Do you not mean something like this?


Which can be used like this?


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
J Brewer
Ranch Hand

Joined: Jul 10, 2006
Posts: 46
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

Joined: Feb 07, 2005
Posts: 2367
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
 
subject: enum static methods