Partner – Moderne – NPI EA (cat=Spring Boot)
announcement - icon

Refactor Java code safely — and automatically — with OpenRewrite.

Refactoring big codebases by hand is slow, risky, and easy to put off. That’s where OpenRewrite comes in. The open-source framework for large-scale, automated code transformations helps teams modernize safely and consistently.

Each month, the creators and maintainers of OpenRewrite at Moderne run live, hands-on training sessions — one for newcomers and one for experienced users. You’ll see how recipes work, how to apply them across projects, and how to modernize code with confidence.

Join the next session, bring your questions, and learn how to automate the kind of work that usually eats your sprint time.

Course – Black Friday 2026 – NPI EA – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

1. Introduction

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.

2. Life Without Getters and Setters 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?

  • Marking the variable as public, protected, or default
  • Changing the value using a dot (.) operator

Let’s look at the consequences of doing this.

3. Accessing Variables Without Getters and Setters

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.

4. Significance of Getters and Setters in Java

Out of many, let’s cover some of the most important benefits of using getters and setters:

  • It helps us achieve encapsulation which is used to hide the state of a structured data object inside a class, preventing unauthorized direct access to them
  • Achieve immutability by declaring the fields as private and using only getters
  • Getters and setters also allow additional functionalities like validation, error handling that could be added more easily in the future. Thus we can add conditional logic and provide behavior according to the needs
  • We can provide different access levels to the fields; for example, the get (read-access) may be public, while the set (write-access) could be protected
  • Control over setting the value of the property correctly
  • With getters and setters, we achieve one more key principle of OOP, i.e., abstraction, which is hiding implementation details so that no one can use the fields directly in other classes or modules

5. Avoiding Mistakes

Below are the most common mistakes to avoid while implementing getters and setters.

5.1. Using Getters and Setters With Public Variables

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.

5.2. Assigning Object References Directly in the Setter Methods

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());
}

5.3. Returning Object References Directly From the Getter Methods

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.

5.4. Adding Unnecessary Getters and Setters

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.

6. Conclusion

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

Partner – Moderne – NPI EA (tag=Refactoring)
announcement - icon

Modern Java teams move fast — but codebases don’t always keep up. Frameworks change, dependencies drift, and tech debt builds until it starts to drag on delivery. OpenRewrite was built to fix that: an open-source refactoring engine that automates repetitive code changes while keeping developer intent intact.

The monthly training series, led by the creators and maintainers of OpenRewrite at Moderne, walks through real-world migrations and modernization patterns. Whether you’re new to recipes or ready to write your own, you’ll learn practical ways to refactor safely and at scale.

If you’ve ever wished refactoring felt as natural — and as fast — as writing code, this is a good place to start.

Course – Black Friday 2026 – NPI EA – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

6 Comments
Oldest
Newest