• 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

I thought this enum code sample would give a compile error

 
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I thought this enum code sample would give a compile error:



Why should I use TimeZone.CST in the switch and CST in the case statement?

(It's question 8 of chapter 5 of the book)
 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

switch (TimeZone.CST) { // should not / cannot be CST Why?
You can't just write CST because the compiler doesn't know it's part of the enum.
You could have written the switch statment like this :

TimeZone tz = TimeZone.CST;
switch (tz) { ... }



case CST: s+= "c"; // should not / cannot be TimeZone.CST Why?
You can't write TimeZone.CST. In fact the case labels inherit the type of the variable passed in the switch so the compiler know the type of CST, for example.

if you try to compile something like ... case TimeZone.CST : ... you get a compiler error like this :
"an enum switch case label must be the unqualified name of an enumeration constant"

I hope this helped you a little.
 
Marc Wentink
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

In fact the case labels inherit the type of the variable passed in the switch



This I did not know, and it surprises me a little. Ok, so it's something specific for the switch in combination with enum? I cannot think of another example in which the switch would inherit some type information since the other possible arguments are the 'integer and smaller' primitive types. And I cannot think of another statement where a block of code inherits type information of the variable passed in. Are there any?
 
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 Marc Wentink:
I cannot think of another example in which the switch would inherit some type information

In every switch statement, the case statements inherit the type information, but it's not so obvious.
 
Manfred Klug
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 Manfred Klug:
In every switch statement, the case statements inherit the type information, but it's not so obvious.

You have to use an explicit cast if you insist on using that value.
 
Marc Wentink
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks!
 
reply
    Bookmark Topic Watch Topic
  • New Topic