File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Question about local variables in switch 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 » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Question about local variables in switch " Watch "Question about local variables in switch " New topic
Author

Question about local variables in switch

Arsineh Keshishian
Greenhorn

Joined: Apr 21, 2008
Posts: 19
Hi,

When running this code:

int n = 4
final int x = 3;
final int y = 4;


I'M getting a compiler error because of j using in case y.
The compiler complains that "j may have not been initialized yet". But I can not reinitialize j either because of "duplicate local variable j".
So the only way is to make j final in case x: final int j = 0;

Why does j has to be final?

Thank you
Arsineh
Ravikanth kolli
Ranch Hand

Joined: Feb 10, 2008
Posts: 179

do you have a global variable "j"?


-kolli
 
 
subject: Question about local variables in switch
 
Similar Threads
AutoBoxing - Compilation Error
Array overwritten
Case constant must be compile time constant?
Can't a switch use final parameters?
j++ IN switch()