• 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 about Enum Class Qualified

 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think the enum must be allways used with the enum class qualifier, for example
MyEnum x = MyEnum.ABC // correct
MyEnum x = ABC // incorrect

But there is a exception in switch, the enum must not be qualified there, for example
switch(x) {
case ABC: ... // correct
case MyEnum.ABC: ... // incorrect
}

Is there any other exceptions about the qualified enum?
thanks
 
Greenhorn
Posts: 29
Android Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I really liked the question, tried googling it...

Here is one link that has this discussed, I still would like someone to explains this detail...

http://stackoverflow.com/questions/4401743/why-final-static-int-can-be-used-as-a-switchs-case-constant-but-not-final-sta
 
Ranch Hand
Posts: 820
IntelliJ IDE VI Editor Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This ensures type safety. If you were allowed to qualify the reference, you could not be sure that what you are evaluating is the same as the type you declared in the switch statement. Or, more importantly, the compiler could not be sure.




EDIT/JD: code block was too wide
 
Ranch Hand
Posts: 300
Eclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Thanks Tim, though could you please elaborate this line , I think here compiler know the Type of variable x as cleric then what's the problem to assign CARDINAL to it ?

cleric x = CARDINAL; // WRONG. doesn't know the symbol and if it did, it wouldn't know which enum

 
Tim McGuire
Ranch Hand
Posts: 820
IntelliJ IDE VI Editor Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

If everything in the left-operands type contract can be met through the contract of the right-operand type, then the assignment will work. It doesn't matter if the right-operand type implements more than the left-operand type; as long as it implements what the left-operand type contract guarantees.



"CARDINAL" alone is not recognized period, so the discussion is moot. If it was recognized, without a qualifier to identify the "namespace" of CARDINAL, the right side of the assignment is not living up to the agreement.
 
Willie Smits can speak 40 languages. This tiny ad can speak only one:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic