Are Getters and Setters mandatory or facilitators?

I've been reading some Java books lately, but there's a part that makes me confused about this kind of accessor methods - gettters and setters.

The question is:

Am I required to write in this kind of methods e.g. getName() or do I just have to write getName() because it makes life easier for the programmer to better serve Object-Oriented Programming? Is the way to make the getters or setters a norm or a convention?

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

Author: Math, 2014-09-24

4 answers

Is the way to make the getters or setters a norm or a convention?

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 facilitating 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 accessor method that picks up the property should start with get . Example: getName();
  • if the property is a boolean , the accessor method that takes the property can start with both get and is . Example: isStarted() or getStarted();
  • the accessor method that assigns a value to the property must start with set . Example: setName();
  • the accessor methods (getters and setters) must be written in the default camelCase ;
  • to compose the name of an accessor method the first word must be either get , or set , or is , and the remainder of the method name must be exactly the same as the Attribute name;
  • methods setters must be return audiences void ;
  • methods getters must be public, have no parameters, and have the return type that matches that of the property.
 23
Author: Math, 2015-04-25 16:34:54

You can write as you like, it 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.

In this way, attributes that can be changed and retrieved externally, it is recommended to use private with getter and setter, as you may want to control this manipulation, thus making it easier to maintain the code.

And the default naming is really for ease of maintenance by other developers, we imagine you want to reuse the class in another project, or pass it on to someone else to use in your project, the default is getAtt and setAtt.

To complicate, can we facilitate?

 17
Author: luigibertaco, 2014-09-24 13:58:19

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

Regarding naming, you should always create the getters and setters the way @Math spoke in his answer, but it's not always necessary to create getters and setters for your object's attributes. You can create immutable objects, and from parameters in Constructors or factories you already create it with all its attributes made, as 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 knows how to deal better with smaller objects that live less time than large objects that change state constantly.

 6
Author: Lucas Polo, 2014-09-27 12:59:13

Is used only in object-oriented languages following the principle of encapsulation.

I recommend reading:

Http://en.wikipedia.org/wiki/Mutator_method

Some interesting answers can also be seen here: https://stackoverflow.com/questions/10407877/what-is-the-point-of-getters-and-setters

As to the way of implementation (the use of prefixes get/is or set) is a universal standard for the identification of this category of methods (a very practical example is those of the Beans used in JSF).

 3
Author: Cold, 2017-05-23 12:37:26