Skip to main content
Lesson 7 - More About Methods
Lesson MenuPreviousNext
  
Lifetime, Initialization, and Scope of Variables page 6 of 10

  1. Three categories of Java variables have been explained thus far in this curriculum guide.

    • Instance variables
    • Local variables
    • Parameter variables

  2. The lifetime of a variable defines the portion of run time during which the variable exists.

    1. 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.
    2. 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.
    3. When a method is called, its parameter variables are created. They stay alive until the method returns to the caller.

  3. The initial state of a variable is also determined by its type.

    1. 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.
    2. Parameter variables are initialized with copies of the formal parameters.
    3. 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.

  4. Scope refers to the area of a program in which an identifier is valid and has meaning.

    1. 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.
    2. The scope of a local variable extends from the point of its definition to the end of the enclosing block.
    3. The scope of a parameter variable is the entire body of its method.

  5. 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
  6. 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.

  7. The statement st.printLocalTest() calls the method printLocalTest , and in a similar way each method is called.

  8. The results show the following about the scope of the variable test:

    1. Within the scope of main, the value of test is 10, the value assigned within the main method.
    2. Within the scope of printLocalTest, the value of test is 20, the value assigned within the printLocalTest method
    3. 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
    4. 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

Lesson MenuPreviousNext
Contact
 ©ICT 2003, All Rights Reserved.