2

I know the benefits of using getters/setters for any attributes in Java when accesing them from outside the class, but do these best practices include internal accesses?

It is common to see constructor methods setting variables by this.foo = foo, like this:

public class Person {
    private int age;
    private String firstName;
    private String lastName;

    public Person(String firstName, String lastName, int age) {
        this.age = age;
        this.firstName = firstName;
        this.lastName = lastName;
    }

    // getters & setters...

What should I do if I need to access an attribute from inside the class?

Thank you in advance.

EDIT: Thank you for all your comments. It seems to be a matter of opinion, providing that there seems not to be a 'best practice' for this case.

LMV_86
  • 37
  • 5
  • `setter` can be overriden (if not final), direct variable assignment can´t, and as though i´d use the direct access. [some oppinions can be gathered from here](https://stackoverflow.com/questions/19445737/calling-getters-on-an-object-vs-storing-it-as-a-local-variable-memory-footprin) – SomeJavaGuy May 31 '17 at 08:03

3 Answers3

1

setters & getters are offering a good mechanism to encapsulate some members of the class(i.e. no other object can from outside read nor writte those without using the right methods for that), since you are in the class, there is no restriction over your own members, you can read/write the member with or without the setter/getter, so use direct access instead!

ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
  • I am not in full agreement with you. If one wants to do something whenever a member is being read/changed, then calling setters and getters even in internal access is superior. I would advocate their usage in the first place, because I consider this approach superior. The down-side is that a function call is being added to the stack, but we can survive it. The upside is that when the setter and getter is changed, there will be no bugs due to forgotten refactorization. – Lajos Arpad May 31 '17 at 08:17
  • 1
    Hi @LajosArpad, thanks for the feedback! – ΦXocę 웃 Пepeúpa ツ May 31 '17 at 08:18
1

The main concept of Encapsulation is to restrict other objects from accessing your class members directly. That is why the members are declared as private and the getters/setters are set to public.

So any class that wants to access the members are only allowed to using getters/setters.

Whereas if you want to do some operation on the variables internally, you can access them directly using this such as this.foo etc. Using getters/setters inside the same class is unnecessary although not something that should not be done.

You can find more about getters/setters here

v1shnu
  • 2,211
  • 8
  • 39
  • 68
1

Setters and getters are methods which have the main purpose of accessing a member and changing its value in case of setters and return its value in case of getters. Example:

public class User {

    protected String name;

    public void setName(String name) {
        this.name = name;
    }

    public String getName(String name) {
        return this.name;
    }

    public User(String name) {
        this.name = name;
    }
}

In general terms there is nothing wrong in accessing the name member directly to get or set its value, however, it is much more elegant to use setters and getters to do so even in the case of internal access. If we take another look at the example above, we will still not directly see the benefit of using setters and getters. However, in the course of time these methods might be changed and they might have further benefits. For instance you might want to save a log record each time the user's name was changed. If you have a lot of this.name = "foo" then you will need to change all of them to this.setName("foo") to get the benefit of the setter. You can avoid this need for refactoring by seeing the potential that the setter might be not so simple in the future and writing superior code in the first place. The same goes with getters. One might want to do something else inside the getter as well. Example: one might want to log when the getter was called and see the call stack for example.

Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175