views:

141

answers:

6

I'm looking for the official naming convention in Java regarding accessors.

I've seen that, for instance, the JPanel class deprecated the size() method in favor of getSize().

But in the ArrayList class, the method is size().

So I'm wondering if accessors should be named getXXX() or xXX() ?

+1  A: 

For anything trying to look like a JavaBean, it should be getXXX or isXXX. (I can't remember whether hasXXX is valid for Boolean properties as well... not sure.)

It makes sense to treat a JPanel in a bean kind of way - for designers etc - but not an ArrayList.

Personally I tend to use the getXXX form just for consistency, but I believe the above is the reasoning involved in ArrayList's naming.

Jon Skeet
A: 

In Eclipse the convention is definitely to use the getpattern. The automisation tools create and handle getters by checking for and writing get and set style accessors.

theseion
+2  A: 

With query methods, I always look at getXXX as something that is provided versus something that is calculated. The size() method returns the size of the collection which is a derived value, so it makes sense. If you had getSize() my assumption would be that I could somehow set the size (through a constructor or setter method).

Javid Jamae
You make a good point but I would say `getSize()` makes more sense compared to `size()` can be considered a (albeit virtual) property of a `List` since it has always a size assigned to it.
Johannes Wachter
+5  A: 

It's usually a bad idea to not use the JavaBeans convention (getters and setters).
They're used by many frameworks and in particular with EL you can't access your fields without the rights getters.

So your accessors should always be named getXxx() or isXxx() and setXxx().

The example of size() in the collection framework is an example of "flaw" which can disturb developers (see link below). In fact this was a choice from Josh Bloch and Neal Gafter to make it more readable.

But remember the JavaBeans convention isn't the Java naming convention.


Resources :

On the same topic :

Colin Hebert
(+1) I'm amused that the [*Naming* Conventions page](http://www.oracle.com/technetwork/java/codeconventions-135099.html#367) is titled "NO TITLE", heh.
Tim Stone
A: 

I prefer the get/is/set-Convention (especially for ValueObjects/DataObjects) not only because it's the JavaBeans specification but because of the following two points.

  1. The clear prefix of the method seperates property related methods from 'logic' methods.
  2. You can use it out of the box with JSP and other frameworks that assume this naming.
Johannes Wachter
+1  A: 

This is only a formative addentum to Colin HERBERT's answer which, in my opinion, is enough:

  • Accessor method signatures should always look like public Type getProperty(). Additionally, accessors should always return a copy of the property's value, not the value itself.
  • Mutator method signatures should always look like public void setProperty(Type value)

Combining an accessor and a mutator gives you a JavaBean property. JavaBeans are not considered to be immutable by nature, but if you want to make it immutable, you should use the following signature for the mutator method: public YourJavaBean withProperty(Type value). Note that this should always return a completely new YourJavaBean instance with copied property values.

Esko