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 Simple Question about enums 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 "Simple Question about enums" Watch "Simple Question about enums" New topic
Author

Simple Question about enums

Erandika Withanage
Greenhorn

Joined: Apr 08, 2011
Posts: 14

Hi! I'm doing K&B6.

I've got a small prob with enums in the first chapter. Could someone please help me out?

In the example in pg.63-64, the enum is declared outside the class and this enhanced for loop works fine.



What about when the enum is declared inside a class? How do you declare the above for loop to do the same thing? I've tried different ways, but something is wrong all the time.

Please help and thank you in advance
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4240
    
    7

Can you show us an example of it not working? Then we can tell you where you're going wrong.
Enkita mody
Ranch Hand

Joined: Aug 06, 2012
Posts: 333

Have a look ItDoesntWorkIsUseless


OCA7
Erandika Withanage
Greenhorn

Joined: Apr 08, 2011
Posts: 14

This is what I tried:

Karthikeyan Kandasamy
Greenhorn

Joined: Apr 17, 2011
Posts: 14
I think, You have to refer the enum with enclosing class all the time.

Made the following change (highlighted) and able to run the program.

for(Coffee2.CoffeeSize1 cs: Coffee2.CoffeeSize1.values()){





Output:
8
BIG 8
HUGE 10
OVERWHELMING 16
Don Redd
Ranch Hand

Joined: Jan 05, 2012
Posts: 82

enums declared inside class are implicitly static, so what ever the rules applied to static nested classes, applies to enum inside class, and in this example when you are accessing nested class(enum) outside enclosing class, you have to use <enclosingclassName>.

DOn...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Simple Question about enums
 
Similar Threads
regarding enums
Cannot understand enum class syntax
Question about redefining Enum
sharing enum contants
enum public/protected/default/private