Is a boolean property name prefixed by “is” still a valid Java Bean?

删除回忆录丶 提交于 2019-12-06 21:46:03

问题


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


回答1:


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




回答2:


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




回答3:


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




回答4:


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.




回答5:


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.



来源:https://stackoverflow.com/questions/17066477/is-a-boolean-property-name-prefixed-by-is-still-a-valid-java-bean

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!