This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Local Variable Scope Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Local Variable Scope" Watch "Local Variable Scope" New topic

Local Variable Scope

Craig Oliver

Joined: Aug 25, 2003
Posts: 21
From what I understand:
Local (method) variables can be declared anywhere in code of method.
They are then available for rest of method.

Question is:
If local variable is declared within try/catch exception block is scope of this variable only within try/catch block or entire method from then on?
Vicken Karaoghlanian
Ranch Hand

Joined: Jul 21, 2003
Posts: 522
You can refer to (�14.4.2) for more information about the scope of local variables.

- Do not try and bend the spoon. That's impossible. Instead, only try to realize the truth. <br />- What truth? <br />- That there is no spoon!!!
Babar Qadri
Ranch Hand

Joined: Oct 12, 2004
Posts: 51
Only within try/catch block. And refer the link given by Vicken.

Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
The key word is block.

Variables declared within a block { ... } are only known within that block.
The individual try, catch, and finally blocks are examples of this.

Mike Gershman
SCJP 1.4, SCWCD in process
It is sorta covered in the JavaRanch Style Guide.
subject: Local Variable Scope