Local Variables and definitionsTopDragging TshirtsParameters vs. Instance Variables

Parameters vs. Instance Variables

Parameters are used to pass information from one object (or method) to another. Call of move in onMouseDrag of Drag2Shirts:

    selectedShirt.moveBy( point.x - lastPoint.x, 
                          point.y - lastPoint.y)

communicates to the moveBy method of selectedShirt the information about how far to move. The argument or actual parameter refers to the information being sent to the object. In this example there are two actual parameters: point.x - lastPoint.x and point.y - lastPoint.y. These are associated with the formal parameters, xOffset and yOffset of the method:

    method moveBy (xOffset: Number, yOffset: Number) { ... }

When you send a message to an object, it is as though we provide a def statement associating the formal parameters with the value of the actual parameters. In the above example, it is as though we executed:

    def xOffset = point.x - lastPoint.x
    def yOffset = point.y - lastPoint.y

Thus the formal parameters refer to the same values as the actual parameters (whether the actual parameters are themselves variables or complicated expressions, as in the above case) at the time of the call.

The matching up of actual and formal parameters is determined solely by the order in which the parameters are written, not by names. Also, the type of an actual parameter must always match the type declared for the formal parameter.

The key differences between parameters and definitions is that parameters are used to transmit information to a method that it needs. The information comes from whatever object is sending the message. Definitions and instance variables, on the other hand, save information inside an object so that it is available again when it is needed. Look back at each of the example methods we wrote and determine why the parameters were needed.


Local Variables and definitionsTopDragging TshirtsParameters vs. Instance Variables