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


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP 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.
 
 
subject: variable initialization within case block