CS 051 Fall 2012

Lecture 7

Review

The "this" keyword in Java is how an object refers to itself. We saw
"this" in our BBall class, when we wanted to implement the moveTo method using
the move method that we had already defined:

   public void moveTo( double x, double y )
   {
       this.move ( x - body.getX(), y - body.getY() );
   }

We also saw it in the solution to Homework 6.9.1, when we overloaded our constructor
method. We wrote a constructor that took x and y points to locate our
StickMan. Instead of repeating all that code for a constructor that worked with a
Location object, we simply used:

   public StickMan( Location loc, DrawingCanvas canvas ) {
       this( loc.getX(), loc.getY(), canvas );
   }

Drag 2 Shirts Example

We looked at the Drag2Shirts example. When dragging two items around the screen, it became
convenient to know which was the selected item, and which was not. We used instance
variables selectedShirt and otherShirt to keep track, and switched which objects these
names referred to if the user clicked on otherShirt.

This example is very similar to some of the work that needs to be done in this week's Magnet Lab.

Parameters and Instance Variables

Instance variables are used to save information about an object. They are declared in the class
file before the method definitions. Currently, we label all of our instance variables as
private (this will change in the future). The private keyword tells Java that
these variables can only be seen inside the class.

Parameters are used to pass information from one objecet (or method) to another. For example,
our call of the move method in onMouseDrag of Drag2Shirts:

selectedShirt.move(point.getX() - lastPoint.getX(),
point.getY() - lastPoint.getY());

The actual parameter refers to the information being sent to the object. In the above example
there are two actual parameters:

   point.getX() - lastPoint.getX()

and

  point.getY() - lastPoint.getY()

These are associate with the formal parameters, xOffset and yOffset of the method:

  public void move(double xOffset, double yOffset) {

This association is done based on order. That is, the first actual parameter is associated with
the first formal parameter, and so on... In our example above, it is as if we are executing the
following assignment statements:

  xOffset = point.getX() - lastPoint.getX()
  yOffset = point.getY() - lastPoint.getY()

Formal parameters are only usable inside the method that defines them.

Local Variables

There are times when we may want additional variables to make our calculations easier. Often,
these variables are only used in one method, and are used to hold intermediate values for our
calculations. Since they are only used in one method, it is not appropriate to make them instance
variables. Since they are not being passed in (shared) from some other method or object, it is not
appropriate to make them parameters.

In this case, we would create a new local variable. Local variables are declared in a similar
way to instance variables and parameters -- we need to give them a name, and tell Java what type
they will be.

Unlike instance variables, they are declared inside a method.

Unlike formal parameters, they are not declared in the method header, but rather they are
declared in the body of the method.

Like formal parameters, they can only be used within the method that defines them, and in some
cases, may be even more limited than that. For this reason, the labels private and
public are not applicable and are never used with local variables.