Skip to the content.
Intro Anatomy of a Class Constructors Accessor Methods Mutator Methods Static Variables Game

Unit 5 Classes - Mutator Methods

Mutators/Setters

Mutators are used to modify the attribute of an object. They are typically public methods to allow external code to modify the object state.

Naming & Parameters

Mutators are named in the setBlank syntax where “Blank” is the name of the field you want to modify. Mutators, like other methods in Java, take one or more parameters.

Return Type

Since mutators are type void as they do not return a value but rather they modify the object’s state.

Validation

Mutators often include validation logic to ensure that the new values are being set within acceptable bounds. An example of validation being used with mutators can be seen with the setSpeed mutator that doesn’t allow negative values.

Mutators Diagram

Lets take a look at an example!

This example discusses showcases some of the most vital use cases for mutators

// A class representing a Car with attributes like make, model, and speed.
public class Car {
    private String brand;
    private String model;
    private int speed;

    // Constructor to initialize attributes
    public Car(String brand, String model, int speed) {
        this.brand = brand;
        this.model = model;
        if (speed >= 0) {  
            this.speed = speed;
        } else {
            System.out.println("Speed cannot be negative, setting speed to 0.");
            this.speed = 0;
        }
    }
    //Mutators 
    public void setBrand(String brand) {
        this.brand = brand;
    }
    public void setModel(String model) {
        this.model = model;
    }
    public void setSpeed(int speed) {
        if (speed >= 0) { //Validation so speed is not negative
            this.speed = speed;
        } else {
            System.out.println("Speed cannot be negative.");
        }
    }
    // Display car details
    public void displayCarInfo() {
        System.out.println("Brand: " + brand);
        System.out.println("Model: " + model);
        System.out.println("Speed: " + speed + " mph");
    }
}
Car myCar = new Car("Honda", "Civic", 100);
myCar.displayCarInfo();

// Modifying attributes mutators
myCar.setSpeed(150);
myCar.setBrand("Rolls Royce");
myCar.setModel("Phantom");
myCar.displayCarInfo();

//Testing Validation with invalid value (Should trigger warning)
myCar.setSpeed(-50); 
Brand: Honda
Model: Civic
Speed: 100 mph
Brand: Rolls Royce
Model: Phantom
Speed: 150 mph
Speed cannot be negative.

Popcorn Hack

1. Create a Class

Define a class Person with private fields: String name, int age, and double height. This is done for you.

2. Create Mutators

Write mutator methods setName(String name), setAge(int age) (with validation for non-negative age), and setHeight(double height) (with validation for non-negative height).

3. Write a Constructor

Create a constructor that initializes name, age, and height with provided values.

4. Test the Mutators

In the main method, create a Person object, modify its fields using the mutators, and print the details after each modification.

public class Person {
    // Private fields
    private String name;
    private int age;
    private double height;

    // Constructor to initialize attributes
    public Person(String name, int age, double height) {
        this.name = name;
        this.age = age;
        this.height = height;
    }

    // Mutators
    public void setName(String name) {
        this.name = name;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public void setHeight(double height) {
        if (height >= 0) { // Validation so height is not negative
            this.height = height;
        } else {
            System.out.println("Height cannot be negative.");
        }
    }

    // Display person details
    public void displayPersonInfo() {
        System.out.println("Name: " + name);
        System.out.println("Age: " + age);
        System.out.println("Height: " + height + " in");
    }
}
Person person = new Person("Vibha", 16, 64);
        person.displayPersonInfo();
person.setAge(150);
person.setAge(16);
person.setHeight(65);
person.displayPersonInfo();
person.setHeight(-50);

Name: Vibha
Age: 16
Height: 64.0 in
Name: Vibha
Age: 16
Height: 65.0 in
Height cannot be negative.