
Baeldung Pro comes with both absolutely No-Ads as well as finally with Dark Mode, for a clean learning experience:
Once the early-adopter seats are all used, the price will go up and stay at $33/year.
Last updated: January 16, 2024
Getters and Setters play an important role in retrieving and updating the value of a variable outside the encapsulating class. A setter updates the value of a variable, while a getter reads the value of a variable.
In this tutorial, we’ll discuss the problems of not using getters/setters, their significance, and common mistakes to avoid while implementing them in Java.
Think about a situation when we want to change the state of an object based on some condition. How could we achieve that without a setter method?
Let’s look at the consequences of doing this.
First, for accessing the variables outside a class without getters/setters, we have to mark those as public, protected, or default. Thus, we’re losing control over the data and compromising the fundamental OOP principle – encapsulation.
Second, since anyone can change the non-private fields from outside the class directly, we cannot achieve immutability.
Third, we cannot provide any conditional logic to the change of the variable. Let’s consider we have a class Employee with a field retirementAge:
public class Employee {
public String name;
public int retirementAge;
// Constructor, but no getter/setter
}
Note that, here we’ve set the fields as public to enable access from outside the class Employee. Now, we need to change the retirementAge of an employee:
public class RetirementAgeModifier {
private Employee employee = new Employee("John", 58);
private void modifyRetirementAge(){
employee.retirementAge=18;
}
}
Here, any client of the Employee class can easily do what they want with the retirementAge field. There’s no way to validate the change.
Fourth, how could we achieve read-only or write-only access to the fields from outside the class?
There come getters and setters to your rescue.
Out of many, let’s cover some of the most important benefits of using getters and setters:
Below are the most common mistakes to avoid while implementing getters and setters.
Public variables can be accessed outside the class using a dot (.) operator. There is no sense in using getters and setters for public variables:
public class Employee {
public String name;
public int retirementAge;
public void setName(String name) {
this.name = name;
}
public String getName() {
return this.name;
}
// getter/setter for retirementAge
}
In this case, anything that can be done with getters and setters can also be done by making the field simply public.
As a rule of thumb, we need to always use the most restricted access modifiers based on the need to achieve encapsulation.
When we assign object reference directly in the setter methods, both these references point to a single object in memory. So, changes made using any of the reference variables are actually made on the same object:
public void setEmployee(Employee employee) {
this.employee = employee;
}
However, we can copy all the elements from one object to another object using a deep copy. Due to this, the state of this object becomes independent of the existing (passed) employee object:
public void setEmployee(Employee employee) {
this.employee.setName(employee.getName());
this.employee.setRetirementAge(employee.getRetirementAge());
}
Similarly, if the getter method returns the reference of the object directly, anyone can use this reference from the outside code to change the state of the object:
public Employee getEmployee() {
return this.employee;
}
Let’s use this getEmployee() method and change the retirementAge:
private void modifyAge() {
Employee employeeTwo = getEmployee();
employeeTwo.setRetirementAge(65);
}
This leads to the unrecoverable loss of the original object.
So, instead of returning the reference from the getter method, we should return a copy of the object. One such way is as below:
public Employee getEmployee() {
return new Employee(this.employee.getName(), this.employee.getRetirementAge());
}
However, we should also keep in mind that creating copies of objects within the getter or setter might not always be a best practice. For example, calling the above getter method in a loop could result in an expensive operation.
On the other hand, if we want that our collection should remain unmodifiable, it would make sense to return a copy of the collection from a getter. We then have to determine which approach suits best in a certain situation.
By having getters and setters, we can control the access and assignment of the member variables. But, in many places, it turns out to be unnecessary. Moreover, it makes the code verbose:
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
Simply defining public getters and setters for a private field in a class is equivalent to making the field public without getters and setters. So, it’s always advisable to decide thoughtfully whether to define accessor methods for all the fields or not.
In this tutorial, we discussed the pros and cons of using getters and setters in Java. We also discussed some common mistakes to be avoided while implementing getters and setters, and how to use those appropriately