boolean (Boolean) - getter is vs get
Asked Answered
H

2

14

It looks like everyone says that right getter for:

  • primitive boolean -> getter is
  • object Boolean -> getter get

Example:

public class Test {

    private boolean primitive;
    private Boolean object;

    public boolean isPrimitive() {
         return primitive;
    }
    public Boolean getObject() {
        return object;
    }
    //..
}

Question:

Is there any spec or document that states this is correct and this is the way to specify getters for boolean values? Or this is only a common assumption?

I'm asking becouse for example wsimport generates getter is for Boolean object. Is this a tool bug, or this is allowed and correct?

In the other hand some framweorks don't work properly with such getters. For example JSF (EL) or Dozer.

Habitancy answered 18/2, 2014 at 10:59 Comment(6)
The rudest name for a reference variable is objectSnyder
is in my opinion only works if the boolean name is an adjective and not a noun.Agglomerate
@CeilingGecko Lots of libs would disagree. Usually when dealing with abstract/super types ... Gson, for example ... JsonElement.isJsonArray()Monotype
I sometimes use has when is doesn't really sound right XDAnastomosis
@user1600770: me too. But only in places where nothing expects the class to follow bean specifications.Niggling
upvoted because I stumbled upon this problem while using dozer (mapping framework expects "get..." for Boolean Object)Bacchanal
R
12

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.

Rondel answered 18/2, 2014 at 11:3 Comment(1)
... let alone that the spec doesn't even mention boolean attributes ;)Jennijennica
C
10
// "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).

Clerical answered 18/2, 2014 at 11:7 Comment(1)
We are you not choosen as the correct answer, I can only wonder By the way lombok have the same reasoning as youPsalmody

© 2022 - 2024 — McMap. All rights reserved.