next up previous
Next: Example: scope of local Up: Unit 03 Previous: Local Variables

Scope of local variables

The scope of a variable is the region of a program in which the variable is visible, i.e., in which it is accessible by its name and can be used.

In Java, the scope of a local variable is the body of the method in which it is declared. In other words, the variable is visible in the body of the method where its declaration appears, but it is not visible on the outside the method.

Clearly, as we have already said in Unit 2, a variable cannot be used in the body of a method before it has been declared.

Notes:


next up previous
Next: Example: scope of local Up: Unit 03 Previous: Local Variables