Incremental Java
Local Variables and Scope

Local Variables, Scope, and Lifetime

In a class definition, there are three kinds of variables. Let's look at an example:
public class TwoSides
   int side1, side2 ;
   public boolean testRightTriangle( int hypoteneuse )
      int side1Squared = side1 * side1 ;
      int side2Squared = side2 * side2 ;
      int hypSquared = hypoteneuse * hypoteneuse ;

      return side1Squared + side2Squared == hypSquared ;
In this example, which checks if we have a right triangle (the class contains the two short sides of a triangle, and we pass the long side, i.e., the hypoteneuse). We apply Pythagorean's theorem to check if it's a right triangle.

In this example:

Local Variables Have No Memory

When you exit a method, both the local variables and the parameter variables disappear (although, if you pass an object as a parameter, you usually have access to the object when you exit the method).

That means if you make a call to testRightTriangle() and run the declarations, it creates new boxes and initializes them. When you exit, those local variables disappears.

When you call it a second time, it recreates the local variables, and reinitializes them.

Parameters behave somewhat like this. These variables gets initalized to the arguments.

Only instance variables maintain their value over method calls. That is, if some instance variable has value of 4 for height, and you change it to 10, then the next time you make a method call, it's still 10. It stays 10 until some method call changes the value.


Different kinds of variables have different lifetimes. Parameter variables exist when the method call is made, and lasts until the method call is complete. These parameter variables hold boxes to values or handles. If the box holds handles (i.e., the parameter variable is an object variable), then when the box disappears, the handle may still stay around. That's usually because you have some variable around that holds the handle, and was passed to the method as an argument.

Local variables also have a similar lifetime. The boxes are created when the declaration appears, and lasts until you exit the method.

Instance variables have a much longer lifetime. They are created when the object is constructed, and goes away when the object disappears. The object disappears when it is no longer being used, which basically means that no variable has a handle to the object. When that happens, the garbage collector gets rid of the object.

The garbage collector is a program that runs when your program runs, and looks for objects (balloons) with no variables that have handles to it.

Scope vs. Lifetime

Scope refers to the range of code where you can access a variable. You can literally print out your program, and draw a box to show where a variable is valid. It's textual in nature. We can divide scope into

Lifetime refers to the amount of time a variable (or object) exists. Since object variables are both a box (holding a handle) and an object, the lifetimes may differ. An object variable could disappear (for example, a local variable disappears once you exit the method where the local variable is declared), yet the object still exists.

We can divide lifetime into categories too: