• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

question on enums

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm studying for my SCJP Exam an i have a question concerning enums and static fields:

I tried to increment a static variable from an enum-constructor. It doesn't compile. Look at the code:

enum BeerSize {

SMALL(300), MEDIUM(500), BIG(1000);

private int volume;
private static int sizeCount=0;

BeerSize(int vol) {
sizeCount++; // this doesn't compile
this.volume = vol;
}

public void setSize(int vol) {
this.volume = vol;
}

public int getSize() {
return this.volume;
}

public void printSize() {
System.out.println("This means "+this.volume+" ml of beer");
}
}

the compiler won't let me access the static variable "sizeCount" from within the constructor.
Can anybody tell me why ? In a "normal" class it works fine...

greetz from hamburg, germany

Jens
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic