• 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

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.
 
They weren't very bright, but they were very, very big. Ad contrast:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic