Search⌘ K

Mutator Methods

Explore how mutator methods in Java manage and update private instance variables within classes. Understand why these void methods are essential for modifying object state safely, especially in AP Computer Science contexts, and practice using setters to interact with class attributes correctly.

What is a mutator method?

A mutator method is often a void method that changes the values of instance variables or static variables.

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

Explanation

Here is an example:

Java
public class TesterClass
{
public static void main(String args[])
{
Person person = new Person("Mathew");
person.name = "James"; // ❌ Error: name is private
System.out.println(person.getName());
}
}

...