next up previous
Next: Method Parameters Up: Varieties of Variables Previous: Varieties of Variables

Local vs. Instance Variables

So far we've talked about local variables and instance variables. How are these related?

Take a look at the chart on p. 120 of Nino & Hosch. A similar explanation appears below:

  Local Variables Instance Variables
Where Declared Within method Within Class Definition
Lifetime While method is executing While object exists
Scope From declaration to end of containing block
private: Any method in class
public: Package that contains class, packages that have access to this package
Useful for Intermediate values need only during execution of method Representing properties of object

There are a few terms in the table above that we haven't defined yet. First of all, what is the scope? Scope is the region of the program where a variable can be referred to. In the case of a local variable, that region is the declaration of the variable to the end of the containing block.

But what is the containing block? The containing block is the stuff in the first level of { } brackets that contain the declaration. For example the containing block for the statement int age = 25; is shown in bold in the following piece of code:

public class BlockExample {
    public static void main(String[] args) {
         int age = 25;
         System.out.println("Age = " + age);
         }
}

Therefore the scope of age is the entire method main.

The containing block for int age = 25; is again shown in the bold below. Note that the scope for age has been reduced to just one line! As a result, the line age++; gives a compiler error: this statement tries to increment age but it is not within the scope of the variable age. Because this statement is not within the scope of age, we cannot refer to age here.

public class BlockExample {
    public static void main(String[] args) {
       { int age = 25; }
         System.out.println("Age = " + age);
         age++;
     }
}

In the case of instance variables, the scope can be controlled by specifying the keyword public or private in the variable declaration within the class definition. The former gives wider access than the latter. A discussion of what a package is and how scope works for public fields will come later. For now, let's stick to private instance variables, and note that we can only refer to such variables from methods in the same class.

What is the Lifetime of a variable? Well, this is the period of time during which memory for the variable is actually allocated. In the case of a local variable, the lifetime begins at the declaration (and therefore at the beginning of the scope) and ends at the end of the method in which the variable was declared (the end of the scope). So is the lifetime the same as the scope in this case? Not exactly. First of all, the memory for the variable age in the second example above is still allocated until the end of the method main, even though it is out of scope as soon as execution passes beyond the declaration. This is a fine point because even though the memory is still allocated, we can't access it since we are out of scope. For now we can ignore this distinction; just remember that the variable is only 'usable' inside its containing block.

Secondly, the execution of the method main might involve calling other methods. When we start executing other methods, the memory for age would remain allocated, but it would be out of scope -- we wouldn't be able to use the word age to refer to this memory while we were executing the other method. However, once we return from the other method to main, we would once again be within the scope of age again.

The best way to remember all of this is to remember the memory model. Recall that local variables are allocated storage within the frame of the currently executing method on the run-time stack, and that whenever we complete a method, the frame for that method is deleted from the stack. If we add a rule that says 'only local variables in the current frame are within scope' then we can see that executing a new method moves us out of scope of the old local variables, and completing that new method removes its frame, leaving us back at the frame of the first method. Thus within the lifetime of a local variable of the 'old' method, we can move out of the variable's scope by calling a new method, and move back into its scope after completing the new method.

The lifetime of an instance variable is the lifetime of the object in which it resides; this can be the entire program. We'll talk more about this later.


next up previous
Next: Method Parameters Up: Varieties of Variables Previous: Varieties of Variables
Chris Trendall
Copyright ©Chris Trendall, 2001. All rights reserved.

2001-12-09