This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Please explain why enums must not be declared within a method. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Please explain why enums must not be declared within a method." Watch "Please explain why enums must not be declared within a method." New topic
Author

Please explain why enums must not be declared within a method.

indra negi
Ranch Hand

Joined: Aug 12, 2009
Posts: 68

Please help me in understanding this concept of not declaring an enum inside a method. This is just because, otherwise it will have a limited scope or there is some other reason.


Thanks in advance
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9293
    
  17

From the JLS

Nested enum types are implicitly static. It is permissable to explicitly declare a nested enum type to be static.

This implies that it is impossible to define a local enum, or to define an enum in an inner class


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Lucas Smith
Ranch Hand

Joined: Apr 20, 2009
Posts: 804
    
    1

You can treat an enum like a class. Local classes can be declared only as final or abstract, while enum is static.


SCJP6, SCWCD5, OCE:EJBD6.
BLOG: http://leakfromjavaheap.blogspot.com
indra negi
Ranch Hand

Joined: Aug 12, 2009
Posts: 68

Thanks a lot Ankit and Lukas.

I got the concept.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Please explain why enums must not be declared within a method.