4

I'm wondering about why to use getter and setter methods in java. If you can read or modify values of private fields via getters and setters, why not just changing them directly to public? I know that getters and setters have to be reasonable, but what would be the straight answer that it's not possible to give up on them?

  • this is according to OOP. you set the rules for accessing them. no one can access them without these rules. – Arpit Feb 21 '13 at 13:31

5 Answers5

6

Some reasons are:

  • You can add additional constraints and checks. For instance, if a particular integer attribute must be in the range [10..20], you can easily add a check to the setter to ensure that no other values are set on the attribute.
  • You can make them "read-only" by providing a getter only.
  • You can easily set a source breakpoint on the setter to see when and from where the value is modified.
  • You can easily add logging to trace which values are ever set.

See the answers in the link provided by @Aditi for additional reasons: Why use getters and setters?

Community
  • 1
  • 1
Andreas Fester
  • 36,091
  • 7
  • 95
  • 123
2

One of the hardest forms of bug to track down is an unexpected, inappropriate change in the value of a data item.

If the data is public, the code that makes the change could be anywhere in the program.

If it is private, the code that makes the change must be in the same source file, typically the same class. It is easy to find and instrument all code that could possibly change it, including its setter.

Patricia Shanahan
  • 25,849
  • 4
  • 38
  • 75
2

This is the way to separate interface from implementation (1) and prepare for changes (2). Maybe simple example can help.

(1)
Let's say taht you have a class User that stores firstName and secondName in separate fields. Now you can have method getFullName which concatenates fisrtName and secondName however the programmer who is using the User class doesn't have to know that firstName and secondName are two separate fields.

(2)
Now you want to store in User class fullName in one field so all you have to do is make changes in the User class and you don't have to chagne all calls of getFullName method, what you would have to do in case if you were using fields directly.

BlueLettuce16
  • 2,013
  • 4
  • 20
  • 31
1

It's good practice to make your fields private to preserve encapsulation in your Java classes. Nothing in the application should know the "inside" of your class, which is implementation details. The only thing they should need to access is methods on your object, to request values or execute processes.

In some cases, you may want to give access to some of the internal values to another class. In this case, it is recommended to do it through a getter, and not directly. This way, refactoring will be a lot easier when you need to change the contract of that class, and/or its implementation details (how you store the values internally).

Also, as it's a common pattern to use getters/setters, this makes your code more easily readable by external readers or co-workers, and some third-party libraries expect them to be there.

Guillaume
  • 22,694
  • 14
  • 56
  • 70
0

By using getters and setters instead of directly accessing the member variable, - We could do the data validations.

For e.g.

public void setAge(){
  if(age < 0){
     //throw exception
  }
}
Jaydeep Rajput
  • 3,605
  • 17
  • 35