Reputation: 27027
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()
?
Upvotes: 5
Views: 3676
Reputation: 748
It is always better to follow setXXX and getXXX pattern as per JavaBeans specification. The size() method named so, may be as it is just querying the state.
Upvotes: 0
Reputation: 93177
It's usually a bad idea to not use the JavaBeans convention (getters and setters).
They're used through reflection by many frameworks, in particular with EL where sometimes you can't access your fields without the rights getters (depending on the EL flavour).
So your accessors should always be named getXxx()
or isXxx()
and setXxx()
.
size()
in the collection framework is an example of "flaw" which can annoy developers (see link below). The choice made by Josh Bloch and Neal Gafter to make it more readable now makes it difficult to obtain in some contexts (EL).
But remember the JavaBeans convention isn't the Java naming convention.
Resources :
On the same topic :
Upvotes: 11
Reputation: 29377
This is only a formative addentum to Colin HERBERT's answer which, in my opinion, is enough:
public Type getProperty()
. Additionally, accessors should always return a copy of the property's value, not the value itself.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.
Upvotes: 1
Reputation: 2735
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.
Upvotes: 0
Reputation: 9009
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).
Upvotes: 3
Reputation: 5125
In Eclipse the convention is definitely to use the get
pattern. The automisation tools create and handle getters by checking for and writing get
and set
style accessors.
Upvotes: 0
Reputation: 1500665
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.
Upvotes: 1