• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

Simple Question about enums

 
Greenhorn
Posts: 14
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:)
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 333
Firefox Browser Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have a look ItDoesntWorkIsUseless
 
Erandika Withanage
Greenhorn
Posts: 14
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is what I tried:

 
Greenhorn
Posts: 14
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 82
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
You're not going crazy. You're going sane in a crazy word. Find comfort in this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic