CS 5 Spring 2000
Lecture 14
April 19

Scope of Variables

A number of people have been having difficulty understanding the relationship between instance variables, local variables, and parameters. The program Scope.java and the class TestClass.java are designed to help you decide if you understand these topics.

An overview:

An instance variable is declared right after the class declaration. So in the TestClass we have four instance variables: x, y, z, w. Unless they are declared static (worry about that later!), a copy of each variable is created for each object of this class that is constructed. It stays around until the object is garbage collected.

A local variable is defined within a method. Usually we define them at the top of the method, but in Java (unlike Pascal) they can be declared ANYWHERE in the method. At least in CodeWarrior they must be declared before they are used. A local variable is created each time the method is called. It disappears the moment the method finishes. Therefore the next time you call the method you get a different copy of the variable, and the old value is lost. Values that need to be saved between method calls should be saved in instance variables, not local variables. The local variable in the method test is called y.

A parameter is a local variable that gets initialized when the method is called. It is initialized to the value of the corresponding argument that appears in the call to the method. These arguments follow the method name between parentheses. The arguments and parameters are matched up left to right.

When a variable name appears in a program it is important to be able to figure out which variable that refers to. If there is an object before the variable followed by a dot (e.g. first.x) then the variable is the instance variable in that object. (Note that it has to be declared public to be referenced this way from outside of the class!) If there is no name and dot before the variable name, Java first asks if it is a local variable or parameter of the current method. If so it uses it. If not, it looks to see if it is an instance variable for the class where the method is defined. If so it uses that. If not, the variable is undefined. (Exception: if the current class is an inner class, it looks next at the instance variables of the enclosing class where the inner class is defined.)

Go through the program Scope.java, predicting the outcome. Then verify it by running the program.

As you call each method, create a "Stack frame" which contains boxes for all local variables and parameters. For parameters, copy in the value of the corresponding argument from the call. If it is a method call to a non-static method (that is, it does not have the modifier static in the method declaration), it is associated with a specific object in the class, usually the one before the dot in the call. Include a box labeled "this" that refers to that object.

Remove that stack frame when the method returns. It is easiest to place these stack frames one under another as they are created, because they get removed in the opposite order that they get created, so the frame at the bottom has the currently active local variables. This "stack" of frames is called the run time stack.

When a new object is created draw a big box containing smaller boxed for all the instance variables. Return a reference to the newly created object as the value of the new. This box should be drawn to one side of the run time stack, because it is not part of this stack. Only variables holding references to objects are on the run time stack, not objects themselves.

Arrays of Object

Go over the last section from last lecture, which we did not get to.

Multidimensional arrays

In a multidimensional array, you can think of the array entries as themselves being arrays. For example, suppose we have the declaration int [][] bozo = new int[4][5]; Here, bozo is an array of 4 entries, each of which is an array of 5 entries. Each of these entries is an int. Equivalently, think of bozo as a 2-dimensional array with 4 rows and 5 columns:

Note how the entry in row i and column j is denoted bozo[i][j]. In some other languages, you might write bozo[i,j], but not in Java or C/C++. You must always use a separate set of brackets for each index: bozo[i][j].

There are some other subtleties with multidimensional arrays, but they can be quite useful.

A 2-dimensional array of rectangles

Entries of multidimensional arrays can be objects. In RectArray2D.java, we have a 6 x 8 array box of Rect objects. It is created by the declaration: Rect [][] box = new Rect[ROWS][COLUMNS]; The left side creates a box which can hold a reference to the array. The right side actually constructs an array with ROWS rows and COLUMNS columns.

As you might expect, since each entry is an object, we can invoke a member function with it. For example, to draw all the rectangles in their current color, we write

for (int row = 0; row < box.length; row++) for (int column = 0; column < box[row].length; column++) box[row][column].fill(page); Note that the expression box.length gives the number of rows in the array. Each of these rows can be thought of as an array itself, so box[row}.length gives the number of columns in the row specified by row.
Scot Drysdale <scot@cs.dartmouth.edu>
Last modified: Wednesday, April 19, 2000