Are Getters and Setters mandatory or facilitators?

Asked

Viewed 1,788 times

24

Lately I have read some Java books, but there is a part that makes me confused in this type of methods accessors - gettters and setters.

The point is:

I am required to write in this type of methods, for example, getName() or just have to write getName() because it makes life easier for the programmer to better serve object-oriented programming? The way to make getters or setters is a norm or convention?

If I write, for example, porName() instead of getName() the compiler does not declare a syntax error and fulfills the same function, only the method name is changed.

  • 7

    Gets and Sets were agreed, as well as method name start with lowercase letter and other things.

  • 3

    Related questions: that one and also that one.

  • 3

    @Luizvieira If this question is really about getters/setters in Java, the correct answer would be Math. Now if we are talking about software design, the answer would be much wider rs

  • 2

    @wryel Truth. I only found it relevant to quote the other questions because this question also questions whether they are 'facilitators''.

  • 2

    In addition to @emanuelsn’s comment, some frameworks (or frameworks species) use this convention to do their job (JSF uses and is an option in Hibernate). In my vision this is the answer: is a convention to facilitate communication, and is a functional convention for some frameworks. The answer to the title of the question, which has nothing to do with its content, is: use getters and setters is not mandatory. We use getter to allow an attribute to be read outside the class and we use Setter to allow it to be modified outside the class.

4 answers

24

The way to make getters or setters is a norm or convention?

It is a convention determined by the company itself that maintains the language, Oracle, as you can see in: Javabeans Standard

The document Javabeans spec determines in addition to this numerous other conventions, all of them with the aim of facilitate communication between developers. Javabeans are Java classes that have properties. Properties are instance variables with the modifier private.

Some of the conventions that refer to class properties are:

  • If the property is not a Boolean, the access method that takes the property must start with get. Example: getName();
  • If the property is a Boolean, the access method that takes the property can start both with get as with is. Example: isStarted() or getStarted();
  • The access method that assigns a value to the property must start with set. Example: setName();
  • Access methods (getters and setters) should be written in the default camelCase;
  • To compose the name of an access method the first word must be or get, or set, or is, and the rest of the method name must be exactly equal to the attribute name;
  • Methods setters should be public with return void;
  • Methods getters must be public, have no parameters and have the type of return that corresponds with that of the property.
  • 2

    Upvote. One thing I’ve noticed in the last few days around here is an overabundance of responses with official references losing space for low-quality responses without any official reference.

  • 2

    Hey Math! Sun, not Oracle (tiozao moment that still has gifts of Sun events at home). :)

  • @wryel Although the only thing not quite right in the answer is precisely the reference, since Javabeans was a specific item of Java EE architecture, nowadays already quite old-fashioned and falling into disuse (the answer itself, of course, is correct - conventions apply to any Java class that wants to use getters and setters).

17

You can write as you like, you can be

obj.colocaValorNoNome() 

or anything, including, if your attribute is public, you don’t even need to use a get and set method, using, for example:

obj.nome = "";

However, thinking about code standardization, best practices and security, the idea is to prevent yourself or another programmer from doing something wrong with the object.

This way, attributes that can be changed and recovered externally, it is recommended to use privately with getter and Setter, as you may want to control this manipulation, thus facilitating the maintenance of the code.

And the standard naming is really to facilitate maintenance by other developers, we imagine that you want to reuse the class in another project, or move to someone else to use in your project, the default is getAtt and setAtt.

Why complicate, if we can facilitate?

  • 3

    Good answer. What would be helpful is to say that many frameworks use getters and setters to access related entities, such as JSF, with their Beans.

6

Some frameworks rely on getters and setters to be able to better reflect upon their classes. As reflection, understand as metaprogramming, widely used in dependency injection or in persistence frameworks (JPA, Hibernate and etc).

When it comes to naming, you should always create the getters and Setter the way @Math spoke in their response, but it’s not always necessary to create getters and setters for your object attributes. You can create immutable objects, and from parameters in constructs or factories you already create it with all its attributes made, for example in the Circle class below:

package encurtador;

public class Circulo {
    private double raio;

    public Circulo(double raio) {
        this.raio = raio;
    }

    public double getRaio() {
        return raio;
    }

    public double getArea() {
        return raio * raio * Math.PI;
    }
}

It becomes much simpler to work in this way, creating methods already related to the function of your object. If you need a circle with another diameter, it is simpler to create another object. This way you can create a pool of objects and the virtual machine can handle smaller objects that live less time than large objects that change state constantly.

3

Browser other questions tagged

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