234

Eg.

boolean isCurrent = false;

What do you name its getter and setter?

Tom
  • 15,798
  • 4
  • 37
  • 48
user496949
  • 83,087
  • 147
  • 309
  • 426
  • 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. – Peter Lawrey Mar 16 '11 at 08:41

11 Answers11

322

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

jmj
  • 237,923
  • 42
  • 401
  • 438
  • 13
    Could you point the section of Sun's code conventions where boolean getter names are specifically covered? I could not find it. – Konstantin Pelepelin Mar 29 '17 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 '18 at 08: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 '19 at 06:44
  • 6
    how'd we get from custom name to customer name? ;) – Kartik Chugh Nov 20 '19 at 01:10
  • 8
    @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 '20 at 23:41
  • @Nathan i was totally concentrating on giving the right answer regarding to the getters and setters. Naming the boolean variable does make more sense when it contains conformation words like "has" – Assegd Jan 22 '20 at 10:57
119

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;
Andrew Tobilko
  • 48,120
  • 14
  • 91
  • 142
Narayan
  • 6,031
  • 3
  • 41
  • 45
  • 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 '16 at 09:34
  • 18
    @FranzB. I'd use setHasData(...) – user362178 Jul 14 '16 at 01: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](http://www.oracle.com/technetwork/java/javase/documentation/spec-136004.html) section 8.3. – VCD Aug 01 '16 at 07:16
  • @Andrew hi. When I am using 'is' prefix in my variable and sending the value of that variable from my js file in the data, it is always giving me the value as false. And if I remove 'is' prefix than it is working absolutely fine. What can be the reason for that? Thanks in advance. – Vibhav Chaddha May 24 '19 at 13:32
  • @Learner Ask a separate question and share a link with me, please. I will have a look. I have no idea how you are sending, what you are sending. – Andrew Tobilko May 24 '19 at 13:41
  • 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`. – Maurizio Lo Bosco Aug 22 '19 at 08: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 '19 at 17:25
  • @FranzB. The "hasData()" method would not be bound to a property. It's result would be calculated from the object state. E.g. "checking if data-stream can be read without blocking." The "shouldAbortg" is bad naming since if it was bound to a setter method, it would be "is/SetAbortRequested()" thus would conform to standard convention. – Torben May 05 '20 at 10:35
84

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;
}
Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588
  • 4
    What if the boolean is not a primitive? If it is Boolean should it be a get or is? – Arun Apr 27 '17 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 – Sean Patrick Floyd Apr 27 '17 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. – Al-Mothafar Feb 01 '18 at 08:51
  • 1
    IntelliJ defaults to using `get` prefix when retrieving a `Boolean` vs `is` for a `boolean` – jocull Feb 14 '19 at 15:55
  • 1
    @jocull and that is the correct behavior, according to the JavaBeans specification – Sean Patrick Floyd Feb 15 '19 at 01:45
  • Seems like Eclipse updated its naming convention for booleans: `is prefix should be used for boolean variables and methods.` https://wiki.eclipse.org/Recommenders/CodingConventions#is_prefix_should_be_used_for_boolean_variables_and_methods. – GG. Apr 01 '20 at 05:58
  • I agree with the advice for practical reasons (having a different name for the variable and getter), but not the reasoning. *Is* and *has* are stative verbs, and are fine to use for naming boolean properties. It's active verbs specifically that should be reserved for functions. – Milosz Dec 07 '21 at 10:11
8

I believe it would be:

void setCurrent(boolean current)
boolean isCurrent()
miku
  • 181,842
  • 47
  • 306
  • 310
  • 3
    I like that convention, but conventions don't really matter. The most important is to stick with the one you chose. – Clement Herreman Mar 16 '11 at 08: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. – Sean Patrick Floyd Mar 16 '11 at 08: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. – Clement Herreman Mar 18 '11 at 15:08
6

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;
    }
Jimmy
  • 995
  • 9
  • 18
  • 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();` – Oscar Bravo Mar 15 '23 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.

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.

Tim T
  • 304
  • 2
  • 9
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;
    }
}
Henry S.
  • 432
  • 1
  • 3
  • 8
-3
private boolean current;

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

public boolean hasCurrent(){
    return this.current;
}
Mkne
  • 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. – Al-Mothafar Feb 01 '18 at 08:48
-4
Setter: public void setCurrent(boolean val)
Getter: public boolean getCurrent()

For booleans you can also use

public boolean isCurrent()
Suraj Chandran
  • 24,433
  • 12
  • 63
  • 94
  • 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 05 '14 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.