Java Reference
In-Depth Information
2. Enter lines 62 through 67 as shown in Figure 9-11 on page 556.
TextPad displays the mutator method, setAutoExpires(), for the autoExpires
variable (Figure 9-13). The use of the keyword, this, in line 64 distinguishes
the instance variable from the parameter.
this keyword
references
instance variable
instance variable
parameter
variable
mutator
method
FIGURE 9-13
expired ACCESSOR AND MUTATOR METHODS As illustrated in the pre-
vious steps, accessor and mutator methods typically are grouped in matching
pairs of get and set methods for ease of maintenance. As discussed earlier, a
boolean variable may have an accessor method with a name that begins with is
rather than get. Figure 9-14 displays the accessor method and mutator method
for the expired instance variable of the Password class. The expired instance vari-
able is used to indicate whether or not the current password value has expired.
69
public boolean isExpired ()
70
{
71
return expired;
72
}
73
74
public void setExpired ( boolean newExpired )
75
{
76
expired = newExpired;
77
}
FIGURE 9-14
The accessor method, isExpired(), in lines 69 through 72 returns the value
of the instance variable, expired. The mutator method, setExpired(), accepts
a boolean value from the user program and then, in line 76, assigns it to the
instance variable, expired. Again, no validation of the parameter is required
because the value can be only true or false, both of which are valid values for this
method. Note that using a unique name for the parameter — that is, newExpired
instead of expired — eliminates the need to use the explicit object reference, this,
for the expired instance variable.
The following steps enter code for the accessor method and mutator method
for the expired instance variable of the Password class.
 
Search WWH ::




Custom Search