• 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

Two related questions regarding enums

 
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1). How does this resolve, Please have a look at case statement number one?
public class Enum {

/**
* @param args
*/
public static void main(String[] args) {
CS.CoffeeSizes cs = CS.CoffeeSizes.BIG;
System.out.println(cs);
System.out.println(CoffeeSize.BIG);
makeSelection(cs);
}

static void makeSelection(CS.CoffeeSizes cs) {
switch(cs) {
case BIG: // HERE HOW DOES COMPILER KNOW THAT WE ARE
//REFFEREING TO CS.CoffeeSizes.BIG and not CS1.CoffeeSizes.BIG
System.out.println("do "+cs);
break;
case LARGE:
System.out.println("do "+cs);
break;
default:
System.out.println("default");
}
}
}

class CS {
enum CoffeeSizes {BIG, LARGE, EXTRA_LARGE}
}

class CS1 {
enum CoffeeSizes {BIG, LARGE, EXTRA_LARGE}
}




2)In above code if i replace BIG with CS.CoffeeSizes.BIG , I get a compiler error
"The enum constant CS.CoffeeSizes.LARGE reference cannot be qualified in a case label"
Why do i get this error? SHouldnt this be the correct way of reffering BIG and not just typing BIG without telling the compiler to which enumeration does this "BIG: belong?
 
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by deepu jain:
and not just typing BIG without telling the compiler to which enumeration does this "BIG: belong?

Since your switch variable is of type CS.CoffeeSizes, you telling the compiler which enum you want.
 
reply
    Bookmark Topic Watch Topic
  • New Topic