Time estimate: 45 min.

5.5. Mutators / Setters

As we saw in the last section, since we typically make instance variables private, we have to define getters if we want to allow code outside the class to access the value of particular instance variables.

By the same token, if we want to allow code outside the class to change the value of an instance variable we have to provide what is formally called a mutator method but which everyone actually calls a setter. A setter is a void method with a name that starts with set and that takes a single argument of the same type as the instance variable to be set. The effect of a setter, as you would probably expect, is to assign the provided value to the instance variable.

Just as you shouldn’t reflexively write a getter for every instance variable, you should think even harder about whether you want to write a setter. Not all instance variables are meant to be manipulated directly by code outside the class.

For example, consider the Turtle class. It provides getters getXPos and getYPos but it does not provide corresponding setters. There are, however, methods that change a Turtle’s position like forward and moveTo. But they do more than just changing the values of instance variables; they also take care of drawing lines on the screen if the pen is down. By not providing setters for those instance variables, the authors of the Turtle class can assume the a Turtle’s position won’t change other than by going through one of the approved movement methods. In general, you shouldn’t write a setter until you find a real reason to do so.

5.5.1. How to write a setter

Here are some examples of how to write a setter for an instance variable:

class ExampleTemplate
{
    // Instance variable declaration
    private typeOfVar varName;

    // Setter method template
    public void setVarName(typeOfVar newValue)
    {
        varName = newValue;
    }
}

Here’s an example of the Student class with a setter for the name variable:

class Student
{
    // Instance variable name
    private String name;

    /**
     * setName sets name to newName
     *
     * @param newName
     */
    public void setName(String newName)
    {
        name = newName;
    }

    public static void main(String[] args)
    {
        // To call a set method, use objectName.setVar(newValue)
        Student s = new Student();
        s.setName("Ayanna");
    }
}

Notice the difference between setters and getters in the following figure. Getters return an instance variable’s value and have the same return type as this variable and no parameters. Setters have a void return type and take a new value as a parameter to change the value of the instance variable.

../_images/get-set-comparison.png

Figure 1: Comparison of setters and getters

coding exercise Coding Exercise

Try the Student class below which has had some setters added. Notice that there is no setId method even though there is a getId. This is presumably because in the system this class is part of, while it makes sense for a student to change their name or email, their id should never change.

You will need to fix one error. The main method is in a separate class TesterClass and does not have access to the private instance variables in the `Student class. Change the main method so that it uses a public setter to change the value instead.

Fix the main method to include a call to the appropriate set method.

exercise Check your understanding

Mutator methods do not have to have a name with “set” in it, although most do. They can be any methods that change the value of an instance variable or a static variable in the class, as can be seen in the AP Practice questions below.

5.5.2. groupwork Programming Challenge : Class Pet Setters

Animal Clinic
  1. Copy your Awesome Animal Clinic Pet class from the last lesson into this Active Code window.

  2. Add set methods for each of the 5 instance variables. Make sure you use good commenting!

  3. Test each of the set methods in the main method.

Create a Pet class that keeps track of the name, age, weight, type of animal, and breed for records at an animal clinic with a constructor, a toString method, and getters and setters for each instance variable.

5.5.3. Summary

  • A void method does not return a value. Its header contains the keyword void before the method name.

  • A mutator method or setter is a void method that changes the values of an instance or static variable.

5.5.4. AP Practice

You have attempted of activities on this page