wood burning stoves 2.0*
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
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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: variable initialization within case block
 
Similar Threads
Help with english to morse code program, I have no idea what I'm doing
Robot - can't get symbols: !@#$^&*()_+
switch statement question
Question regarding static
Can I do this with a Switch case?