Java : naming convention for accessors
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 开发者_StackOverflow社区ArrayList
class, the method is size()
.
So I'm wondering if accessors should be named getXXX()
or xXX()
?
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 :
- How to Access the Size of a Collection in a JSP Page Using JSTL-EL
- Java conventions
- JavaBeans
- JavaBeans specification
On the same topic :
- Getters on an immutable type
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).
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.
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.
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.
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.
- The clear prefix of the method seperates property related methods from 'logic' methods.
- You can use it out of the box with JSP and other frameworks that assume this naming.
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.
精彩评论