Quantcast
Channel: Baeldung
Viewing all articles
Browse latest Browse all 3522

Significance of Getters and Setters in Java

$
0
0

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. So, 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
}

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

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

The post Significance of Getters and Setters in Java first appeared on Baeldung.
       

Viewing all articles
Browse latest Browse all 3522

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>