Java Reference
In-Depth Information
Coding Accessor Methods
An accessor method provides the value of a private variable within the class
to a user program. These methods sometimes are called get or getter methods
because of a common naming convention, in which the methods often are
named using the verb, get, followed by the name of the returned instance vari-
able. For example, the accessor method, getMaxHistory(), returns the value of
the maxHistory instance variable. For boolean variables, accessor methods some-
times are coded with the word, is, followed by the variable name, rather than the
word, get. For example, the boolean instance variable, expired, could have an
accessor method named either getExpired() or isExpired().
An accessor method is coded with no parameters; however, its return data
type must match the variable whose value it returns. For example, the
maxHistory variable is of type int; therefore, the header for its accessor method
would be
public int getMaxHistory()
In the body of such an accessor method, the code typically may do no more than
simply return the corresponding variable value, and as such is quite simple. An
accessor method is necessary in order for a user program to obtain the value of
a private instance variable.
Coding Mutator Methods
A mutator method accepts a proposed new value for the variable and
sometimes is called a set or setter method, again because of a common naming
convention. In this case, set is added in front of the variable name to form the
method name, as in setMaxHistory(). With mutator methods, the return type
typically is void, although sometimes it is boolean so the method can indicate
whether the proposed change was accepted. The single parameter represents the
proposed new value for the given variable. For the maxHistory variable, the
mutator method header would be written as
public void setMaxHistory(int newMaxHistory)
The mutator method then is responsible for determining if the new value is
valid — in this case, within an acceptable range. A user program would call the
setMaxHistory() method, sending a proposed new value for the instance vari-
able. If the proposed value is accepted, the mutator method would change the
value of the corresponding instance variable.
The new value is only a proposed value, because the mutator method may
determine that it is not valid and reject it without making the modification. This
action illustrates another aspect of encapsulation: that objects are responsible for
their own data. Encapsulating the data and the methods that access and change
the data within the object allows the object to determine if a new value is valid
before the attribute is changed. For example, the requirements document states
that the number of passwords to keep in the history can be changed, but only
within a range of one to ten. Therefore, the mutator method for this attribute
must verify that any new value is within this range before allowing the current
value to be changed. In many non-object-oriented languages, variables may
be assigned new values from many different places in the program code and
Search WWH ::




Custom Search