Generating Accessors and Mutators

Accessors (getter) methods are used to get the value of instance variable.

Mutators (setter) methods are used to set the value of instance variable.

Eclipse provides an option to generate the accessors (getters) and mutators (setters) for instance variables that have already been defined.

As mentioned earlier in this lab, only create accessors and mutators when appropriate.

To demonstrate how Eclipse can automatically generate these methods for you

  1. Delete the getNumerator and getDenominator methods from your Fraction class.
  2. Right click in the Editor window. select “source” from the drop-down list that appears.
  3. Select “Generate Getters and Setters” in the next drop-down window.
  4. The pop-up window shown below is displayed. Select getDenominator and getNumerator and press “OK”

  5. Code for the getNumerator and getDenominator methods is generated.