What is the correct syntax for "is" variable getter/setters in a POJO class?

Nik Reiman picture Nik Reiman · Jul 13, 2009 · Viewed 13.9k times · Source

If a class contains a variable named "blah", then the standard getter/setter syntax is obviously getBlah() and setBlah(). But if I have a POJO class with a variable named isBlah, would I use:

public type getIsBlah() {
  return isBlah;
}

public setIsBlah(type isBlah) {
  this.isBlah = isBlah;
}

Or would it be this?

public type isBlah() {
  return isBlah;
}

public setBlah(type blah) {
  this.isBlah = blah;
}

The first seems to conform more strictly to the POJO conventions, but the second type is what IntelliJ generates if I ask it to make a class' getter/setters (and hey, IntelliJ has never let me down yet :] ). So which is the preferred syntax?

Answer

Jon Skeet picture Jon Skeet · Jul 13, 2009

One reason for using properties is to decouple the API from the implementation. In other words, you shouldn't feel bound by what your private variable is called. That shouldn't inform the naming beyond trying to keep it readable to code maintainers.

I would say that if "type" is boolean in this case, then the second form is correct. If it's not boolean, you should use getXXX - but I probably wouldn't use getIsXXX. To me, "is" has a very strong correspondence with Boolean properties, and using it in other contexts would not only break the JavaBeans conventions (which could affect other tools) but would be misleading IMO.