Following the theme of articles on object-oriented programming, today I'm looking at how to program accessor and mutator methods in Java. The role of accessors and mutators are to return and set the values of an object's state.
Sometimes it necessary to provide a way to gain access to values that make up the state of a object. The calling code might need to know what the value is or to modify it. The most direct way that still enforces data encapsulation is through the use of accessors and mutators.