Eclipse creating getter with prefix is

Asked

Viewed 98 times

2

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

Could someone help me or explain why?

  • Complementing my question, the declared variable is: private Boolean status;

  • 1

    Perhaps because a Boolean attribute is usually defined to characterize the object as is/is not something. In this case, I would define it as "true state" or "false state", which doesn’t seem to make much sense. Change the name to "active" and you will see that it makes total sense to exist the method isActive.

2 answers

6


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

Maybe with a name attribute status is not the best case. Think about getter of property administrador.

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

It’s semantically better the getters of logical objects having the prefix is in particular cases.

Keep in mind also that the standard getPropriedade and setPropriedade is not a rule.

That doesn’t just apply to getters. Imagine a class Pessoa with an attribute estadoCivil. You see the semantic advantage of Setter of estadoCivil was casar() or divorciar()?

Anyway it is possible to disable this Eclipse setting in Settings > Code Style > Java > Use 'is' prefix for getters that return boolean.

4

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

In the version 1.0.1 specification, more precisely in the section 8.3.2 good practice is specified for boolean properties:

8.3.2 Boolean properties

In addition, for Boolean properties, we allow a 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);

How the property is boolean, then the is improves the reading of getter.

Exemplifying:

  1. A class X has an attribute enabled of the kind Boolean
  2. true is for yes, as well as false is not to
  3. The is in English is equivalent to "is/is" in Portuguese
    1. isEnabled can be translated to English as: "is activated?"

Therefore, we can conclude that your IDE is creating the getters of booleans prefixed is instead of get because it is following the Spec Javabeans

Browser other questions tagged

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