237

Eg.

boolean isCurrent = false;

What do you name its getter and setter?

1
  • 2
    I assume you are refering to JavaBeans in which case @Jigar Joshi's answer is correct. However if you are asking about generic getter/setters, the only convension is that the methods contain the field's name and the getter takes no arguments and returns a value, the setter takes one argument and returns no value or returns the object itself. see Buffer as a example of another approach to getter/setters. Mar 16, 2011 at 8:41

11 Answers 11

325

Suppose you have

boolean active;

Accessors method would be

public boolean isActive(){return this.active;}

public void setActive(boolean active){this.active = active;}

See Also

6
  • 14
    Could you point the section of Sun's code conventions where boolean getter names are specifically covered? I could not find it. Mar 29, 2017 at 17:36
  • 6
    I have a boolean filed named hasCustomName, Now what should i name for it's getter and setter methods? Is setHasCustomName[setter] and hasCustomName[getter] good?
    – Hadi
    Jul 29, 2018 at 8:50
  • @Hadi just name your variable "customerName" and generate getter n setter for it. Expected getter and setters are public boolean isCustomerName(){return this.customerName;} public void setCustomerName(boolean customerName){this.customerName= customerName;}
    – Assegd
    Sep 10, 2019 at 6:44
  • 6
    how'd we get from custom name to customer name? ;) Nov 20, 2019 at 1:10
  • 9
    @Assegd Naming it "customerName" or "customName" is confusing and does not denote that it is boolean. Seeing the variable I'd expect it to contain a name. In this case, it should be called "hasCustomName" IMO.
    – Nathan
    Jan 21, 2020 at 23:41
121

http://geosoft.no/development/javastyle.html#Specific

  1. is prefix should be used for boolean variables and methods.

    isSet, isVisible, isFinished, isFound, isOpen

This is the naming convention for boolean methods and variables used by Sun for the Java core packages. Using the is prefix solves a common problem of choosing bad boolean names like status or flag. isStatus or isFlag simply doesn't fit, and the programmer is forced to chose more meaningful names.

Setter methods for boolean variables must have set prefix as in:

void setFound(boolean isFound);

There are a few alternatives to the is prefix that fits better in some situations. These are has, can and should prefixes:

boolean hasLicense(); 
boolean canEvaluate(); 
boolean shouldAbort = false;
8
  • 9
    So if there is the boolean property hasData, what would the setter look like? Most certainly, setData(bool hasData) looks awfully wrong to me...
    – Franz B.
    Jan 25, 2016 at 9:34
  • 19
    @FranzB. I'd use setHasData(...)
    – user362178
    Jul 14, 2016 at 1:05
  • 4
    For those who want to follows JavaBeans specifcation, it seems that has, can, should prefixes are not part of the specification. Reference JavaBeans Specification 1.01 section 8.3.
    – VCD
    Aug 1, 2016 at 7:16
  • 3
    The setter is straightforward, for the getter I had to use boolean isIsCurrent(){...} otherwise the framework used to deserialize the object, was complaining with getter not found for property isCurrent. Aug 22, 2019 at 8:52
  • 2
    @VCD "it seems that has, can, should prefixes are not part of the specification" - Hence, that's the whole problem with blindly trying to follow these types of specifications. They usually just cover basic scenarios. The main thing is, use intuitive variable and method names that make sense and that people maintaining this code later will understand. Common sense goes a long way here.
    – dcp
    Nov 26, 2019 at 17:25
85

For a field named isCurrent, the correct getter / setter naming is setCurrent() / isCurrent() (at least that's what Eclipse thinks), which is highly confusing and can be traced back to the main problem:

Your field should not be called isCurrent in the first place. Is is a verb and verbs are inappropriate to represent an Object's state. Use an adjective instead, and suddenly your getter / setter names will make more sense:

private boolean current;

public boolean isCurrent(){
    return current;
}

public void setCurrent(final boolean current){
    this.current = current;
}
7
  • 5
    What if the boolean is not a primitive? If it is Boolean should it be a get or is?
    – Arun
    Apr 27, 2017 at 15:56
  • 2
    No, such a method could return null, which would cause a NullPointerException. But I would try to avoid returning Boolean in the first place Apr 27, 2017 at 20:04
  • 3
    @Arun I think it should be set/get instead if set/is because of Boolean is an object instead of primitive, because it has 3 stats, false, true or null. Feb 1, 2018 at 8:51
  • 1
    IntelliJ defaults to using get prefix when retrieving a Boolean vs is for a boolean
    – jocull
    Feb 14, 2019 at 15:55
  • 1
    @jocull and that is the correct behavior, according to the JavaBeans specification Feb 15, 2019 at 1:45
8

I believe it would be:

void setCurrent(boolean current)
boolean isCurrent()
3
  • 3
    I like that convention, but conventions don't really matter. The most important is to stick with the one you chose. Mar 16, 2011 at 8:31
  • 6
    @Clement Conventions do matter when you rely on tools that use these conventions. JavaBeans is a convention with wide support in plenty libraries (JSP / JSF / Spring / Groovy just to name a few). Breaking the conventions means breaking the way these libraries work. Mar 16, 2011 at 8:57
  • 1
    @Sean Right, excepted for framework that rely on conventions over configuration. In this case conventions are imposed by the framework, so you don't chose anything. Good remark. Mar 18, 2011 at 15:08
7

There is a markable point between setter/getter method of the data type Boolean and boolean in side a class ( for pojo/entity).

  • For both Boolean and boolean the setter method should be setXXX() but getter method would be getXXX() and isXXX() respectively

Example:

(a) if property is defines as Boolean

private Boolean check;

the setter/getter method

public Boolean getCheck() {   // getXXX()
    return check;
}

public void setCheck(Boolean check) {
    this.check = check;
}

(b) if property is defines as boolean

private boolean check;

the setter/getter method

   public boolean isCheck() {   // isXXX()
        return check;
    }
    public void setCheck(boolean check) {
        this.check = check;
    }
1
  • 1
    Presumably, this is because if you get a Boolean, you may want to do something complicated with it, like compareTo, or toString. It would be a bit weird to say; myThing.isWibble().toString(). But if you get a boolean, all you can do is test it; if (myThing.isWibble()) doWibble(); Mar 15 at 14:25
5

Maybe it is time to start revising this answer? Personally I would vote for setActive() and unsetActive() (alternatives can be setUnActive(), notActive(), disable(), etc. depending on context) since "setActive" implies you activate it at all times, which you don't. It's kind of counter intuitive to say "setActive" but actually remove the active state.

Another problem is, you can can not listen to specifically a SetActive event in a CQRS way, you would need to listen to a 'setActiveEvent' and determine inside that listener wether is was actually set active or not. Or of course determine which event to call when calling setActive() but that then goes against the Separation of Concerns principle.

A good read on this is the FlagArgument article by Martin Fowler: http://martinfowler.com/bliki/FlagArgument.html

However, I come from a PHP background and see this trend being adopted more and more. Not sure how much this lives with Java development.

0
2

It should just be get{varname} like every other getter. Changing it to "is" doesn't stop bad variable names, it just makes another unnecessary rule.

Consider program generated code, or reflection derivations.

It's a non-useful convention that should be dropped at the first available opportunity.

1

It is highly recommended to use an adjective to name a boolean field. If you generate getter and setter using IntelliJ, you will find out that the getter is isCurrent() for both of boolean fields current and isCurrent.

We can take a look at IntelliJ community source code, its test data shows that no matter whether your boolean field name starts with is or not, the name of getter starts with is.

class Getter {
  boolean foo;
  boolean isBar;
  boolean hasBaz;

  @java.lang.SuppressWarnings("all")
  public boolean isFoo() {
      return this.foo;
  }

  @java.lang.SuppressWarnings("all")
  public boolean isBar() {
      return this.isBar;
  }

  @java.lang.SuppressWarnings("all")
  public boolean isHasBaz() {
      return this.hasBaz;
  }
}

It will be very confusing when you want to call the getter, when you boolean field name starts with is. Besides, when your colleagues want to get the value of a boolean field you defined, they will only know the getter' s name instead of the field' s name. In that case, the prefix is is not necessary.

Here is another example, when I retrieve data from database to instantiate a object of Employee, the value of isRetired is always false. Because Java does not find an appropriate setter, the value of a boolean field is always default value, say false, which is not expected.

class Employee{
    private int age;
    private boolean isRetired;

    ...
    public boolean setRetired(boolean isRetired){
        this.isRetired = isRetired;
    }
}
-3
private boolean current;

public void setCurrent(boolean current){
    this.current=current;
}

public boolean hasCurrent(){
    return this.current;
}
1
  • 4
    has current what? I think has used for BO or such a service with some processing while for POJO it is is. and please add some description about your answer. Feb 1, 2018 at 8:48
-4
Setter: public void setCurrent(boolean val)
Getter: public boolean getCurrent()

For booleans you can also use

public boolean isCurrent()
1
  • 12
    Because the OP states a question about boolean values. A getter prefixed with 'get' is (read: should) never be used for boolean values.
    – Harold
    May 5, 2014 at 14:40
-5

As a setter, how about:

// setter
public void beCurrent(boolean X) {
    this.isCurrent = X;
}

or

// setter
public void makeCurrent(boolean X) {
    this.isCurrent = X;
}

I'm not sure if these naming make sense to native English speakers.

2
  • 2
    They don't really make sence Aug 16, 2018 at 8:25
  • But sounds phonetically maybe promising with some attributes :) Oct 15, 2019 at 20:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.