Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

doubt regarding switch() case construct for flow control

 
Mansukhdeep Thind
Ranch Hand
Posts: 1158
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sir / Madam
I am studying for the SCJP 1.5 exam.(310-055)

Please find below the code:

final int a = 1;
final int b;
b=2;
int x=0;
switch(x){
case a: //compiles fine
case b: // compiler error

My doubt is why does the case b satatement throw compiler error. The book says that the case constants have to be COMPILE TIME CONSTANTS. What exactly are COMPILE TIME CONSTANTS? Moreover, isn't b a constant here as we have initialized it to 2 here? Please clarify.

Regards
Mansukhdeep Singh
Infosys Technologies Ltd. Pune, India.
 
Henry Wong
author
Marshal
Pie
Posts: 21123
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
 
Henry Wong
author
Marshal
Pie
Posts: 21123
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mansukhdeep Thind wrote: What exactly are COMPILE TIME CONSTANTS?


I wrote this a while back... it should help answer your question.

http://www.coderanch.com/t/454384/java/java/compile-time-constant

Henry
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Compile Time Constants = the value of these are known at compile time! Compile time constants are more specific type of Constants!
 
Campbell Ritchie
Sheriff
Pie
Posts: 48972
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And welcome to JavaRanch
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic