Mutator Methods
Learn what a mutator method is and how to write one in Java.
We'll cover the following...
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 privateSystem.out.println(person.getName());}}
OOPS! ...