This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes variable initialization within case block Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "variable initialization within case block" Watch "variable initialization within case block" New topic
Author

variable initialization within case block

Aakash Goel
Ranch Hand

Joined: May 26, 2008
Posts: 198


This works! How is this even possible?


SCJP 5 95%
SCJP FAQ | SCJP Mock Tests | SCJP Tipline | Generics
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
What happens when you run the code with a = 2?


All code in my posts, unless a source is explicitly mentioned, is my own.
Aakash Goel
Ranch Hand

Joined: May 26, 2008
Posts: 198


prints:

Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
Declarations are processed at compile time and do not depend on the execution flow of your code. Since b is declared within the local scope of the switch block, it is useable anywhere in that block from the point of its declaration.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: variable initialization within case block