SwitchTopParameters vs. Instance VariablesLocal Variables

Local Variables

There is a form of name that is like an instance variable, in that its meaning is determined by assignments rather than by being matched with actual parameters, but that is like a parameter in that it is local to a single method. These names are called local variables.

A good example of a variable that should have been declared local is the variables used to generate a new cloth color in part 1 of the Laundry lab. Rather than look at that program, we will look at similar code that appears if we try to modify our SpiroGraph program for use on July 4th (i.e. we make it draw in the three colors red, white and blue)

Notice that declarations of local variables are not labeled as private. Because they are local variables, they are automatically even more restricted than private instance variables. They can only be seen and used within the body of the method they are declared in, and go out of existence when the method terminates.


SwitchTopParameters vs. Instance VariablesLocal Variables