Generate setters only in the class constructor or generate outside the constructor

9

In Java classes, it is common for to create private attributes and to generate getters and setters for these attributes.

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

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

Doing this second option, leaving the setters in the constructor, is it a good practice (or even a best practice) to apply to class building? Or creating gets and sets for all attributes is most recommended?

    
asked by anonymous 08.08.2014 / 15:06

3 answers

7

You may not provide any public setter for an attribute if you wish so, what is not recommended is to leave the attribute public as if one day you decide to change it to private and assigning 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 private and do not make the setter available, there is no problem with it, even if one day you resolve to add a public , you will not be breaking no class like that.

In summary, 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.

    
08.08.2014 / 15:14
6

This should vary depending on the functionality of your class.

What attributes of your class need to be changed right in the constructor? Note that you do not need 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 a parameter.

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

See how it varies from implementation to implementation, goal to goal. However this does not mean that it does not contain setter for an attribute to be best practice or not, it is necessary to evaluate case by case.

Hugs!

    
08.08.2014 / 15:18
5

Although there are already good answers, with good explanations, you had no examples to exemplify the question, so I will 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'Imutavel ', does not have setter and can only be set by the constructor.

Now consider the following class that uses and handles 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 getter public of an attribute and only allow instanciar (setar) the attribute in the constructor, it will not be changeable by the instance, its value can only be set when it is created .

But this is not an incorrect approach; on the contrary, it is often used for specific purposes, for example if you really want to ensure that the attribute can only be set when the object is created.

  

Note: In the example, the 'attributeImutavel' attribute could even use the 'operator' (I do not know if this is the definition) final like this:

private final long atributoImutavel;

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

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

  

Only you have to know / understand why you are doing this.

That's why in college, as you mentioned, you're taught to always create getters and setters , because in 99% of business entity cases this is how it will be, then default is private attribute and getters and setters .

I do not know if it looks 'exemplary'.

    
08.08.2014 / 15:53