File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Simple Question about enums" Watch "Simple Question about enums" New topic

Simple Question about enums

Erandika Withanage

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

Joined: Apr 06, 2010
Posts: 4543

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

Erandika Withanage

Joined: Apr 08, 2011
Posts: 14

This is what I tried:

Karthikeyan Kandasamy

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()){

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>.

I agree. Here's the link:
subject: Simple Question about enums
It's not a secret anymore!