Pregunta

I just noticed something I didn't know.

  private boolean isCertified;

  public boolean isCertified() {
    return isCertified;
  }

  public void setCertified(boolean certified) {
    isCertified = certified;
  }

The following getters and setters have been generated by Intellij. By the way, Lombok generate the same kind of getters and setters.

I would have expected something like:

  private boolean isCertified;

  public boolean isIsCertified() {
    return isCertified;
  }

  public void setIsCertified(boolean certified) {
    isCertified = certified;
  }

That's why I usually don't prefix my boolean attributes with ìs, despites the fact I think the property name becomes more readable.

I normally write something like:

  private boolean certified;

  public boolean isCertified() {
    return certified;
  }

  public void setCertified(boolean certified) {
    certified = certified;
  }

So I wonder:

  • Having a property named isXXX and a getter being isXXX instead of isIsXXX: is it a valid Java Bean definition?

  • Are there any other hidden Java Bean corner cases like that, that I may want to know to improve the code readability?

Thanks

¿Fue útil?

Solución

AFAIK, the naming pattern of fields is not part of the JavaBeans specification.

The JavaBeans Specification specifies (among others) the "properties" concept.

The properties of a class are identified by the methods (named after a certain pattern) of a class.

The fields are irrelevant. In fact, there doesn't even have to be a field for a property.

That said, it's still a good practice to name the fields after the property names. The chance is greater that tools which need to access the fields as well (e.g. refactoring support in IDEs) will handle the fields correctly.

Having a property named isXXX and a getter being isXXX instead of isIsXXX: is it a valid Java Bean definition?

No, a getter for a property isXXX requires isIsXXX() (for boolean) or getIsXXX().

But again it's the other way around.

If you have a method:

boolean isXyz()

then you have a readable property xyz.

If you have a method

boolean isIsXyz()

then you have a readable property isXyz.

For more information have a look at the Introspector class, the tutorial or the JavaBeans Specification:

http://www.oracle.com/technetwork/java/javase/documentation/spec-136004.html

http://www.oracle.com/technetwork/java/javase/tech/index-jsp-138795.html

Otros consejos

private boolean certified;

public boolean isCertified() {
    return certified;
}

public void setCertified(boolean certified) {
    this.certified = certified;
}

It's ok. More info about variables names you can look on oracle.docs

It doesn't really matter what your private fields are called; that's why your setters and getters are there.

Since properties of Java Beans are usually just defined by their getter and setter methods (and the actual, most probably private, member variable is ignored for that purpose), yes, that is perfectly fine. Although I would still call the boolean member just certified, but that's a matter of style/taste.

When you generate setters/getters for boolean variable from IDE (Eclipse in many case), this is the standard way in which the setters and getters are created. It is a perfect valid Java bean definition. Infact when you use frameworks like JSF where values are passed using setters/getters, defining boolean variable and using the generated setter/getter becomes sort of mandatory.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top