boolean (Boolean) - getter is vs get

The getter method for the field boolean myField is getMyfield() or isMyField() (it's up to the user to choose). I personally use the second format, as many source code generating tools do.

This format is a standard, it is defined in the JavaBeans specification. See the section 8.3.2 of this documentation: http://download.oracle.com/otndocs/jcp/7224-javabeans-1.01-fr-spec-oth-JSpec/

Quote from the docs:

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

  public boolean is<PropertyName>();

The documentation doesn't talk about the primitive wrappers like the Boolean class.


// "is" used because the value can be either true or false. It's like asking isTrue?
public boolean isPrimitive() {
     return primitive;
}

// "get" is used because the value returned can be either true, false or null.  
// So, the third state 'null' makes you wonder if 'is' should be used or 'get'.
// "get" is more appropriate as Boolean can also have null.
public Boolean getObject() {
    return object;
}

But frankly, it's left to the developer. There's nothing "wrong" in using getBoolean() on a boolean value (is makes more sense, that's it).

Tags:

Java

Wsimport