This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes doubt regarding switch() case construct for flow control Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "doubt regarding switch() case construct for flow control" Watch "doubt regarding switch() case construct for flow control" New topic
Author

doubt regarding switch() case construct for flow control

Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

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.

~ Mansukh
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18546
    
  40

Please QuoteYourSources


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18546
    
  40

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

Joined: Nov 04, 2009
Posts: 2066

Compile Time Constants = the value of these are known at compile time! Compile time constants are more specific type of Constants!


|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38075
    
  22
And welcome to JavaRanch
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: doubt regarding switch() case construct for flow control
 
Similar Threads
assignment problem
Case constant must be compile time constant?
Switch- Case doubt in case expressions.
Doubt about Switch statement (Compile Time Constant)
case doubt