| |
Lifetime, Initialization, and Scope of Variables | page 6 of 10 |
Three categories of Java variables have been explained thus far in this curriculum guide.
- Instance variables
- Local variables
- Parameter variables
The lifetime of a variable defines the portion of run time during which the variable exists.
- When an object is constructed, all its instance variables are created. As long as any part of the program can access the object, it stays alive.
- A local variable is created when the program enters the statement that defines it. It stays alive until the block that encloses the variable definition is exited.
- When a method is called, its parameter variables are created. They stay alive until the method returns to the caller.
The initial state of a variable is also determined by its type.
- Instance variables (associated with a particular object) and static variables (associated with a particular class) are automatically initialized with a default value (
0 for numbers, false for boolean , null for objects) unless you specify another parameter.
- Parameter variables are initialized with copies of the formal parameters.
- Local variables are not initialized by default. An initial value must be supplied. The compiler will generate an error if an attempt is made to use a local variable that has never been initialized.
Scope refers to the area of a program in which an identifier is valid and has meaning.
- Instance variables of a class are usually declared
private , and have class scope. Class scope begins at the opening left brace, {, of the class definition and terminates at the closing brace, }, of the class definition. Class scope enables methods of a class to directly access all instance variables defined in the class.
- The scope of a local variable extends from the point of its definition to the end of the enclosing block.
- The scope of a parameter variable is the entire body of its method.
An example of the scope of a variable is given in Program 7-3. The class ScopeTest is created with four methods:
- printLocalTest
- printInstanceTest
- printParamTest
- main
The subclass st is created as "a kind of" ScopeTest , so it contains the same methods. Each of these methods contains a variable named test .
The statement st.printLocalTest() calls the method printLocalTest , and in a similar way each method is called.
The results show the following about the scope of the variable test :
- Within the scope of
main , the value of test is 10 , the value assigned within the main method.
- Within the scope of
printLocalTest , the value of test is 20 , the value assigned within the printLocalTest method
- Within the scope of
printInstanceTest , the value of test is 30 , the private value assigned within ScopeTest , because there is no value given to test within the printInstanceTest method
- Within the scope of
printParamTest , the value of test is 40 , the value sent to the printParamTest method
Program 7-3
public class ScopeTest
{
private int test = 30;
public void printLocalTest()
{
int test = 20;
System.out.println("printLocalTest: test = " + test);
}
public void printInstanceTest()
{
System.out.println("printInstanceTest: test = " + test);
}
public void printParamTest(int test)
{
System.out.println("printParamTest: test = " + test);
}
public static void main (String[ ] args)
{
int test = 10;
ScopeTest st = new ScopeTest();
System.out.println("main: test = " + test);
st.printLocalTest();
st.printInstanceTest();
st.printParamTest(40);
}
}
Run output:
main: test = 10
printLocalTest: test = 20
printInstanceTest: test = 30
printParamTest: test = 40
|