Eclipse creating getter with prefix is

2

I created the methods using the Eclipse Getters and Setters command, but when I created the getter of the status Boolean attribute, it was automatically created as isStatus . Should not be created as getStatus ?

Could anyone help me or explain the reason?

    
asked by anonymous 27.09.2017 / 03:54

2 answers

6

Boolean, logical attributes have improved semantics in the getter when generated automatically by the IDE.

Maybe with an attribute of status is not the best case. Think of the getter property of the administrador property.

private boolean administrador;
public boolean isAdministrador() { ... }

It is semantically better for getters of logical objects to have the prefix is in particular cases.

Also keep in mind that the getPropriedade and setPropriedade pattern is not a rule.

This is not just for getters . Imagine a Pessoa class with a estadoCivil attribute. See the semantic advantage of the setter of estadoCivil be casar() or divorciar() ?

Either way, you can disable this Eclipse configuration in Settings > Code Style > Java > Use 'is' prefix for getters that return boolean.

    
27.09.2017 / 04:00
4

The JavaBeans specification determines patterns to be followed when writing Java objects.

In the 1.0.1 in the 8.3.2 section, good practices are specified for Boolean properties :

  

8.3.2 Boolean properties

     

In addition, for boolean properties, we allow getter method to match the pattern:

public boolean is<PropertyName>();
     

This "is" method may be provided instead of a "get" method, or it may be provided in   addition to a "get" method. In either case, if the   "Is" method is present for a boolean property then we   will use the "is" method to read the property value. An   example boolean property might be:

public boolean isMarsupial();  
public void setMarsupial(boolean m);

As the property is boolean , then is improves the getter reading.

Exemplifying:

  • An X class has an enabled attribute of type boolean
  • true stands for yes, as false stands for
  • is in English is equivalent to " is / is " in Portuguese
  • isEnabled can be translated into Portuguese as: " is enabled? "
  • So we can conclude that your IDE is creating getters of booleans with the prefix is instead of get because you are following the JavaBeans Specification

        
    27.09.2017 / 05:08