aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes enum within a method? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "enum within a method?" Watch "enum within a method?" New topic
Author

enum within a method?

Prash Negu
Ranch Hand

Joined: Apr 20, 2009
Posts: 45
Hi,

I was wondering why we cannot declare an enum within a method when a class can be declared in a method?

Can anybody advise?

Thanks,
Prashant
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39534
    
  27
Only anonymous classes can be declared inside of methods. How would you use an anonymous enum?


Ping & DNS - updated with new look and Ping home screen widget
Moojid Hamid
Ranch Hand

Joined: Mar 07, 2009
Posts: 120
Ulf Dittmer wrote:Only anonymous classes can be declared inside of methods.




It compiles and runs fine.
Devaka Cooray
ExamLab Creator
Saloon Keeper

Joined: Jul 29, 2008
Posts: 2885
    
  13

Both anonymous and local inner classes can be defined inside a method, as Moojid demonstrated in his example.

In Java literature, we can say that enums are implicitly static. Because we can use it as Apple.bigApple, where Apple is an enum type. You know that static declarations (declarations of static variables/static classes) are not valid inside a method. Therefore we couldn't declare enums inside a method local. But this is a *one* reason only. There are some other technical reasons rely on that.


Author of ExamLab ExamLab - a free SCJP / OCPJP exam simulator
My Home Page -- Twitter Profile -- JavaRanch FAQ -- How to Ask a Question
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: enum within a method?
 
Similar Threads
Doubt About Enum in java 1,5
enums
enum within a class
enumerations
String support for switch case statement