Methods: Communicating with Objects
We communicate with objects using methods. Methods are executable code within each object, for which an interface has been established. Sometimes the interface is only for the object itself. Other times it is an interface accessible by other objects. This chapter discusses that topic in detail.
3.2 Passing Information to an Object
Arguments and Parameters
The new class definition for OneRowNim is given in Figure 3.1. Note that
now that we have a single method, takeSticks(), that can be used
to take away a variable number of sticks, we have removed the three
methods we wrote in the previous chapter, takeOne(), takeTwo(), and
takeThree(), from OneRowNim. Using a single method, with a parameter, is clearly a better design. To see this, just imagine what we would have
to do if we didn’t use a parameter and we wanted to be able to take away
four sticks, or five, or more. If we didn’t have parameters, we’d have to
write a separate method for each case, which is clearly a bad idea. Using
parameters in this way leads to a more general useful method and thus is
an example of the generality principle.
Now let’s consider how we would create a OneRowNim instance and
use the new method in the main() method or in a different class. If we
want to have an instance of OneRowNim object to remove 3 sticks on the
first move by using the takeSticks() method, we need to pass the int
value 3 to the method. In order to effect this action, we would use the
Because the definition of takeSticks() includes a single int parameter, we must supply a single int value (such as 3), when we invoke it.
When the method is invoked, its formal parameter (num) will be set to the
value we supply (3). The value we supply does not have to be a literal
int value. We can supply any expression or variable that evaluates to an
int value. For example:
In this case, the value being passed to takeSticks() is 2, the value that val has at the time the method call is made.
It would be an error to try to pass a value that was not a int to takeSticks(). For example, each of the following invocations of takeSticks() results in a syntax error:
As you recall from Chapter 0, the value that is passed to a method when
it is invoked is called an argument. Even though the terms argument and parameter are sometimes used interchangeably, it will be useful to observe a distinction. We will use the term parameter to refer to the formal
parameter—the variable used to pass data to a method—that occurs in the
method definition. We use the term argument to refer to the actual value
that is supplied when the method is invoked.
The distinction between parameter and argument is related to the difference between defining a method and invoking a method. Defining a method
is a matter of writing a method definition, such as
This definition defines a method that takes a single String parameter, and simply prints the value of its parameter. On the other hand, invoking
a method is a matter of writing a method call statement, such as
This statement calls the printStr() method and passes it the string
“HelloWorld”. This notation assumes that the call to the instance method
printStr() is made within the body of another instance method of the