Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I use accessors for field values that will never change?

Let's take this class as an example:

public class Student{
    private String name;
    private String id;

    public Student(String name, String id){
        this.name = name;
        this.id = id;
    }

    ... getters and setters for both fields

and compare it to this:

public class Student{
    public final String name;
    public final String id;

    public Student(String name, String id){
        this.name = name;
        this.id = id;
    }
 }

There is in my opinion no need for the accessors.
Would this be considered bad OO design?

like image 421
rtheunissen Avatar asked Aug 08 '12 00:08

rtheunissen


People also ask

Why do we need accessors?

An accessor method allows other objects to obtain the value of instance variables or static variables. A non-void method returns a single value. Its header includes the return type in place of the keyword void. Accessor methods that return primitive types use “return by value” where a copy of the value is returned.

Should I always use getters and setters?

Encapsulation is one of the core concepts of object oriented programming. Using getters and setters, is always, in my opinion good practice. One thing you should avoid is to have external entities mess with the internal structure of your class at will. Typical example, consider having a dateOfBirth parameter.

Should you use the getter method also whenever you use the setter method?

Getters and setters are used to protect your data, particularly when creating classes. For each instance variable, a getter method returns its value while a setter method sets or updates its value. Given this, getters and setters are also known as accessors and mutators, respectively.

What's the advantage of using getters and setters?

The getter and setter method gives you centralized control of how a certain field is initialized and provided to the client, which makes it much easier to verify and debug. To see which thread is accessing and what values are going out, you can easily place breakpoints or a print statement. 2.


1 Answers

This is a loaded question.

Unfortunately, good (or bad) design is 100% dependent on how it is going to be used.

As a rule of thumb, it is a good idea to keep member variables private, just so that the Object model is in control of their access. This implies the first approach is better.

BUT

if the values never change, what's the point? Why bother writing setters if they will never be used?

So, which one is better? As I mentioned above, that depends on what you are doing this for. If it's for an assignment for class, I would go with the first one. Your teacher will like that more, as it is more "textbook".

So, if this is a personal project or for work where you can take advantage of future releases, I would go with a cross between the two:

public class Student{
    private final String name;
    private final String id;

    public Student(String name, String id){
        this.name = name;
        this.id = id;
    }

  ... getters ONLY for both fields

This approach is safe, because the member fields are private, and there isn't the "code smell" of unused methods. This is also fairly extensible, as you can quite easily add the setters if your requirements ever change and you need to modify the fields.

like image 164
edthethird Avatar answered Sep 17 '22 16:09

edthethird