Java Reference
In-Depth Information
Note that the definition of the method
setTime
checks for the valid values of
hours
,
minutes
,and
seconds
. If any of these values is out of range, the corresponding instance
variable is initialized to
0
. Now let's look at how the method
setTime
works.
The method
setTime
is a
void
method and has three parameters. Therefore:
A call to this method is a stand-alone statement.
We must use three parameters in a call to this method.
Furthermore, recall that because
setTime
is a member of the
class
Clock
, it can directly
access the instance variables
hr
,
min
, and
sec
, as shown in the definition of
setTime
.
Suppose that the object
myClock
is as shown in Figure 8-7.
hr 12
min 36
sec 49
myClock
FIGURE 8-7
Object
myClock
Consider the following statement:
myClock.setTime(3, 48, 52);
The variable
myClock
accesses the member
setTime
.Inthe tement
myClock.setTime(3, 48, 52);
,
setTime
is accessed by the variable
myClock
. There-
fore, the three variables—
hr
,
min
, and
sec
—referred to in the body of the method
setTime
are the three instance variables of the object
myClock
. Thus, the values
3
,
48
,
and
52
, which are passed as parameters in the preceding statement, are assigned to the
three instance variables of the object
myClock
by the method
setTime
(see the body of
the method
setTime
). After the previous statement executes,
myClock
is as shown in
Figure 8-8.
hr 3
min 48
myClock
sec 52
FIGURE 8-8
myClock
after statement
myClock.setTime(3,48,52);
executes
Search WWH ::
Custom Search