Mutator Methods

Learn what a mutator method is and how to write one in Java.

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:

Press + to interact
Person.java
TesterClass.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());
}
}

OOPS! ...