Getters and Setters are obligatory or facilitators?

22

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

The question is:

I'm forced 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? Is the way to make getters or setters a rule or a convention?

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

    
asked by anonymous 24.09.2014 / 15:49

4 answers

22
  

Is the way to get getters or setters a rule or a convention?

It is a company-determined convention that holds the language, Oracle, as you can see at: JavaBeans Standard

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

Some of the conventions that refer to class properties are:

  • If the property is not a boolean , the accessor method that takes the property must 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 getters and setters must be written in the camelCase pattern;
  • To compose the name of an accessor method the first word must be either get , or set , or is , and the rest of the method name must be exactly the same as the attribute name;
  • getters methods should be public, have no parameters, and have the return type that matches that of the property.
24.09.2014 / 16:21
17

You can write as you want, it can be

obj.colocaValorNoNome() 

or anything, even if your attribute is public, you do not even have to use a get and set method, for example:

obj.nome = "";

However, thinking about standardization of code, good 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, because you may want to control this manipulation, thus facilitating code maintenance.

And the naming pattern is actually to make it easier for other developers to maintain, 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.

Why complicate, if we can make it easier?

    
24.09.2014 / 15:58
6

Some frameworks depend on getters and setters to better reflect on your classes. As reflection, understand how metaprogramming, much used in dependency injection or in persistence frameworks (JPA, Hibernate and etc).

Regarding nomenclature, you should always create getters and setters the way @Math spoke in its response, but it is not always necessary to create getters and setters for your object's attributes. You can create immutable objects, and from parameters in builders or factories you already create it with all its attributes made, for example in the class Circle 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 is much simpler to work this way, creating methods already related to the function of your object. If you need a circle with a different diameter, it is simpler for you to create another object. This way you can create a pool of objects and the virtual machine knows how to handle smaller objects that live less time than large objects that constantly change state.

    
27.09.2014 / 14:59
3

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

I recommend reading:

link

Some interesting answers can also be seen here: link

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

    
27.09.2014 / 16:31