This week's giveaway is in the Spring forum.
We're giving away four copies of REST with Spring (video course) and have Eugen Paraschiv on-line!
See this thread for details.
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 REST with Spring (video course) this week in the Spring forum!
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: 4541

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

It is sorta covered in the JavaRanch Style Guide.
subject: Simple Question about enums
It's not a secret anymore!