Naming convention for getters/setters in Java

Solution 1:

The correct answer is

getxIndex()
setxIndex(int value)

if you want them to be used as properties according to section 8.8: Capitalization of inferred names of the JavaBeans API specification (e.g. access them via ${object.xIndex} in a JSP.

Solution 2:

In accordance with JavaBeans API specification from 1997 it should be as Thomas Einwaller describes:

// According to JavaBeans API specification
public int getxIndex() { return xIndex; }
public void setxIndex(int xIndex) { this.xIndex = xIndex; }

This is unfortunate, getx and setx are not words. In the rare case when this would form a word or acronym it would be disinformative, eg the method setiMessage most likely has nothing to do with SETI. Using the only valid measurement of code quality (WTFs per minute), I assess that this is bad code.

If we modify this to follow the convention for naming a method it would be:

// According to Java naming convention
public int getXIndex() { return xIndex; }
public void setXIndex(int xIndex) { this.xIndex = xIndex; }

Why does the JavaBeans specification violate the convention? It all comes down to this sentence of the JavaBeans specification:

However to support the occasional use of all upper-case names, we check if the first two characters of the name are both upper case and if so leave it alone.

Exactly what kind of use of all upper-case names this refers to is unclear to me. Field names should, according to convention, be camel cased. It seems to me that we generate unconventional method names in order to support unconventional field names as decided by a 20+ year old document.

It should also be noted that even though it seems to be an overwhelming support for the JavaBeans specification in tools, it is not exclusively used. Eg. Kotlin will not recognize xIndex as a property in the above example. Reversely, the Kotlin property var xIndex = 0 will result in the Java methods getXIndex and setXIndex. This seems to be a bug according to the JetBrains support, but I fail to see how they can fix that without making a breaking change.

Some tools that does support the JavaBeans specification has not always done so, eg Jackson and Swagger Code Generator have been patched to conform to it. Even though IntelliJ generate accessors according to the JavaBeans specification, the example in the documentation differs from it. Probably because people don't know about the standard and naturally prefers the normal method naming convention.

So when should we follow the JavaBeans specification? When property names should be inferred by accessors by tools that rely on this standard, then we might want to use it. For instance, Jackson will rely on the property xIndex being accessed through getxIndex and setxIndex methods unless we use annotations.

When should we avoid this standard? Per my recommendation: When the code should be read and understood by humans. Because to not use proper camel casing when naming methods is disinformative.

If I would have it my way, we would use normal naming conventions, ie getXIndex and setXIndex. But, given the state of things, the best solution I see is suggested by @vaxquis:

Name your field "indexX" or whatever else, your problem is solved... don't overcomplicate things - even if setxIndex is the correct way for Beans, having method named setxIndex increases the WTF factor of the code without giving you anything in return.

Any comments regarding the JavaBeans specification should, according the specification itself, be sent to [email protected].

Solution 3:

Should be:

getXIndex()
setXIndex(final int xIndex)

Solution 4:

Methods should be verbs, in mixed case with the first letter lowercase, with the first letter of each internal word capitalized.