File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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

Question about local variables in switch

Arsineh Keshishian

Joined: Apr 21, 2008
Posts: 19

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
Ravikanth kolli
Ranch Hand

Joined: Feb 10, 2008
Posts: 179

do you have a global variable "j"?

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()