SCJP題庫第112題

Given:
12. public class Test{
13.   public enum Dogs{collie, harrier);
14.   public static void main(String[] args){
15.     Dogs myDog = Dogs.collie;
16.     switch(myDog){
17.     case collie:
18.         System.out.print("collie ");
19.     case harrier:
20.         System.out.print("harrier ");
21.     }
22.   }
23. }
What is the result?


A. collie
B. harrier
C. Compilation fails.
D. collie harrier
E. An exception is thrown at runtime.
 
Ans: D
 
解說:
myDog = Dogs.collie,所以會進入case collie執行,只是此case沒有設break,後面的case會跟著執行

Comments

老当益壮 said…
Only inside the switch case statements can we ignore the enum Name. In this case, it is valid to use case collie: , not case Dogs.collie:
老当益壮 said…
this has sth to do with the switch /case statements.

Popular posts from this blog

Android+Google Map API v3 Geocoding(地址轉經緯度度