• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Simple Question about enums

 
Erandika Withanage
Greenhorn
Posts: 14
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4566
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you show us an example of it not working? Then we can tell you where you're going wrong.
 
Enkita mody
Ranch Hand
Posts: 333
Firefox Browser Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have a look ItDoesntWorkIsUseless
 
Erandika Withanage
Greenhorn
Posts: 14
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is what I tried:

 
Karthikeyan Kandasamy
Greenhorn
Posts: 14
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 82
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic