File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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
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: 19073
    
  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: 19073
    
  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: 40052
    
  28
And welcome to JavaRanch
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: doubt regarding switch() case construct for flow control