Class 1, Part 2 |
Intro to Java 30-IT-396 |
|
Accessor, Mutator Methods
-
For proper encapsulation, instance fields should be private, and
should only be accessible to other classes via methods.
-
This assists in debugging and data validation.
-
An accessor method is a public method that simply returns the value of
that instance field.
-
It can either return the value itself, or it can perform some manipulation
first. It can also change related fields.
-
A mutator method changes the value of an instance field. It is also
a public method.
-
As a matter of fact, the JavaBeans standard specifies a syntax to do so.
-
The instance fields must be declared private, and the first letter of the
variable name must be lower case.
-
The name of the accessor method starts with get, then the name of the instance
field, capitalized.
-
The name of the mutator method starts with set, then the name of the instance
field, capitalized.
-
For boolean values, the accessor method will often start with is instead.
-
The JavaBeans concept is important.
-
It allows custom GUI components to be added to an IDE.
-
It is also widely used in JSPs.
Cars Example


Created by: Brandan
Jones January 4, 2002