The code will call the enum and get blue as the value, the switch/case block will fall through to the entry for "blue" and output the text given. Then, because there is no break: statement the code will continue through the next case and then exit.
Joined: Sep 12, 2008
BTW, after stepping into this code with the Netbeans IDE, i can confidently say that the way it is written, the switch function sees the value "blue" and jumps right to it, bypassing the "case:red..." and the "default:..." entries. Additionally when i moved the default line to the end of the switch code block, it also outputs "default" as well. Apparently the DEFAULT entry needs to be the very last entry or it will be ignored if bypassed initially. hope this helps. Actually watching code step through its lines using the F7 key is a great learning tool in Netbeans.
Stephen: you said "-my answer is based on the fact that an argument to a switch statement must be compatible with type int (as they explain themselevs in the answer review)"
This used to be 100% correct, but the latest versions of Java also allow enums along with primitives compatible with type int in switch statements
Good Luck, Pat Avery
SCJP 6 88%
skipping SCJD to work on passing SCWCD
Joined: Jul 23, 2008
Thanks Greg & Patrick.
I was more asking of the switch compatibility with enums, I am aware of the break statement etc. Yes you're right as of Java 5 enums can be passed to a switch block.I looked it up in my JAVA Desktop reference (5th ed.) O'Reilly. Its an interesting change, but for the best (if your into switch statements!) :roll:
I'm an eclipse man myself, but using a mac, day to day I love TextMate [ September 14, 2008: Message edited by: Stephen Davies ]