Generate setters only in class constructor or generate outside constructor

Asked

Viewed 407 times

9

In Java classes, it is common (At least in college I only see it that way) create private attributes and generate getters and setters for these attributes.

But I read that you can do something a little different: just generate the getters and leave the setters in the builder.

In my head, leaving the setters in the builder, I think the code will be easier to understand and easier to maintain..

Doing according to this second option, leave the setters in the builder, is it good practice (or even best practice) to apply in class construction? Or creating gets and sets for all attributes is more recommended?

3 answers

7


You may well provide no method Setter public for an attribute if you wish, what is not recommended is to leave the attribute as public, because if one day you decide to change it to private and assign some validation to it you may be breaking the code of other classes that access the attribute directly, without using the Setter.

If you leave the attribute as private and do not provide the Setter there’s no problem with that, even if one day you decide to add a Setter public, because you won’t be breaking any class this way.

In short, you can do this, as long as it is appropriate for your application, you are not required to implement getters and setters at all times.

6

This should vary depending on the functionality of your class.

Do the attributes of this class need to be changed in the constructor? Note that it is not necessary to contain all attributes as parameters of your constructor, it will depend on the purpose of the class. You can set a default value in the constructor for them without having them received as parameter.

Another question would be, can these attributes be changed while running the program? Getters and setters, in short, are tied to the idea of encapsulation.

See that it varies from implementation to implementation, objective to objective. Anyway this does not make not contain Setter for an attribute is best practice or not, it is necessary to assess case by case.

Hug!

5

Although there are already good answers, with good explanations, had no examples to exemplify the question, so I’ll post my answer with a simple example to try to add value to the question.

Considering the following structure of class:

public class ClazzTest {

    private long id;
    private String nome;
    private long atributoImutavel;

    public long getId() {
        return id;
    }
    public void setId(long id) {
        this.id = id;
    }
    public String getNome() {
        return nome;
    }
    public void setNome(String nome) {
        this.nome = nome;
    }
    public long getAtributoImutavel() {
        return atributoImutavel;
    }

    public ClazzTest(long id, String nome, long atributoImutavel) {
        super();
        this.id = id;
        this.nome = nome;
        this.atributoImutavel = atributoImutavel;
    }
}

Where the attribute 'assignable', does not have setter and can only be set by the constructor.

Now consider the following class using and handling the class 'Clazztest':

public class UtilizaClazzTest {

    public void manipulaClazzTest(){
        ClazzTest clazzTest = new ClazzTest(1 /* id */, "Test" /* nome */, 10 /* atributo imutavel */);
        clazzTest.setId(3);
        clazzTest.setNome("Fernando");
        // mas não consigo setar o atributo imutavel
    }
}

So if I only create the getter public of an attribute and only allow instantiation(setar) the attribute in the constructor, it will not be changeable by the instance, its value can only be set at the time it is created.

But, this is not an incorrect approach, on the contrary, it is widely used for specific purposes, for example if you really want to ensure that the attribute can only be set at the time of the creation of the object.

Note: In the example, the attribute 'attributable' could even use the 'operator' (I don’t know if this is the definition) final thus:

private final long atributoImutavel;

Thus you would be ensuring that the attribute would only be set at the time of instantiation and could no longer be set by any internal method of class, making it truly 'imutavel' after its creation.

But so, as you quoted @Math in your answer:

"You can perfectly not provide any public Setter method for an attribute if you so desire"

You just have to know/understand why you’re doing this.

That’s why in college like you said, you’re taught to always create the getters and setters, because in 99% of cases of business entities this is how it will be, so by default it is attribute private and getters and setters.

I don’t know if it was 'exemplifiable''.

  • 1

    Yes. It was very 'exemplifiable' and of course. Thank you!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.