• 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

enum

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
enum CoffeeSize{
BIG(8), HUGE(10), OVERWHELMING(16);
CoffeeSize(int ounce){
this.ounce = ounce;
}
private int ounce;
public int getOunces(){
return ounce;
}

}
class EnumTest {
static CoffeeSize size ;
size = CoffeeSize.BIG;
public static void main(String []args){

// size = CoffeeSize.BIG;
}
}
if I assign size after line
static CoffeeSize size ; I got a complier error "EnumTest.java": <identifier> expected at line 17, column 14
". But i assign in side main there is no compiler error.What is the difference between these two assignments. Why am I getting a compile time error if I write
static CoffeeSize size ;
size = CoffeeSize.BIG;
out side main method.
 
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can't have arbitrary expressions outside of a method or initializer. If you want a variable initializer with the declaration you need to do just that:


[ June 02, 2006: Message edited by: Ken Blair ]
 
I wasn't selected to go to mars. This tiny ad got in ahead of me:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic