Accessors increase robustness of Java code
Scott Ambler explains how to use accessors, and why their use is one of the most important standards your organization can enforce. This article is modified from Chapter 8 of The Object Primer 2nd Edition.
Encapsulation and information hiding are fundamental principles that lead to the design of robust classes and components. An important technique that Java developers can employ to increase encapsulation, and to support information hiding, is the appropriate use of accessor methods.
Accessors — member functions that directly manipulate the value of fields — come in two flavors: setters and getters. A setter modifies the value of a field, whereas a getter obtains its value. Although accessors add minimal overhead to your code, the loss in performance is often trivial compared to other factors (such as questionable database designs). Accessors help to hide the implementation details of your classes and, thus, increase the robustness of your code. By having, at most, two control points from which a field is accessed, one setter and one getter, you are able to increase the maintainability of your classes by minimizing the points at which changes need to be made.
Figure 1 depicts the implementation of the getter and setter member functions for the
homeAddress field of the
Person class. Getters and setters are useful because they enable you to encapsulate important business rules, transformation logic, and validation logic that are applicable to your data fields. For example, Figure 2 presents an alternative implementation of the setter member function. See how it first verifies that the address object it has been passed is valid by invoking its
validate() member function. If it is valid, then the function sets the value of the
homeAddress field. If the address is not valid, the function throws an exception.
One of the most important standards your organization can enforce is the use of accessors. Some developers don’t want to use accessor member functions because they don’t want to type the few extra keystrokes required (for example, for a getter, you need to type in get and () above and beyond the name of the field). The bottom line is this: the increased maintainability and extensibility from using accessors more than justifies their use.
Getter member functions should be given the name get + field name, unless the field represents a Boolean (true or false), and then the getter is given the name is + field name. Setter member functions should be given the name set + field name, regardless of the field type. As you see in Table 1, the field name is always in mixed case with the first letter of each word capitalized. This naming convention is used consistently within the Java Development Kit (JDK) and is required for JavaBeans development and for persistent fields in Enterprise JavaBean (EJB) 2.0.
|Field||Type||Getter name||Setter name|
|instructors||Vector of Professor objects||getInstructors||setInstructors|