Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
A variable's scope is the region of a program within which the variable can be referred to by its simple name. Secondarily, scope determines when the system creates and destroys memory for the variable. Scope is distinct from visibility, which applies only to member variables and determines whether the variable can be used from outside of the class within which it is declared. Visibility is set with an access modifier. See the Controlling Access to Members of a Class section for more information.The location of the variable declaration within your program establishes its scope. There are four categories of scope, as shown in the following figure.
A member variable is a member of a class or an object. It is declared within a class but outside of any method or constructor. A member variable's scope is the entire declaration of the class. However, the declaration of a member needs to appear before it is used when that use is in a member initialization expression. For information about declaring member variables, refer to the Declaring Member Variables section. The four categories of scope: member variable, method parameter, local variable, and exception-handler parameter.
You declare local variables within a block of code. In general, the scope of a local variable extends from its declaration to the end of the code block in which it was declared. In
MaxVariablesDemo
, all the variables declared within themain
method are local variables. The scope of each variable in that program extends from the declaration of the variable to the end of themain
method indicated by the second to last right curly bracket,}
, in the program code.Parameters are formal arguments to methods or constructors and are used to pass values into methods and constructors. The scope of a parameter is the entire method or constructor for which it is a parameter. The Classes and Inheritance chapter discusses writing methods in the Defining Methods section, which talks about passing values into methods through parameters.
Exception-handler parameters are similar to parameters but are arguments to an exception handler rather than to a method or a constructor. The scope of an exception-handler parameter is the code block between
{
and}
that follows a catch statement. The Handling Errors Using Exceptions chapter talks about using exceptions to handle errors and shows you how to write an exception handler that has a parameter.Consider the following code sample.
The final line won't compile because the local variableif (...) { int i = 17; ... } System.out.println("The value of i = " + i); //errori
is out of scope. The scope ofi
is the block of code between{
and}
. Thei
variable does not exist anymore after the closing}
. Either the variable declaration needs to be moved outside of theif
statement block, or theprintln
method call needs to be moved into theif
statement block.
Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
Copyright 1995-2005 Sun Microsystems, Inc. All rights reserved.