Reputation:
I've noticed that for getters that return Boolean
(not boolean
!) netbeans generates getter with "get" prefix. For example:
private Boolean main;
public Boolean getMain(){
return this.main;
}
Is this wrong (according to naming convention)? Or "is" prefix only for primitive type?
Upvotes: 9
Views: 2895
Reputation: 4875
According to OCP Oracle Certified Professional Java SE 8 Programmer II Study Guide book:
Which of the following could be correctly included in a JavaBean?
public boolean isPlaying() { return playing; }
public boolean getPlaying() { return playing; }
public Boolean isDancing() { return dancing; }
The first line is correct because it defines a proper getter for a boolean variable. The second example is also correct, since boolean may use is or get. The third line is incorrect, because a Boolean wrapper should start with get, since it is an object.
Upvotes: 0
Reputation: 421030
It depends on if the class containing the method is to be treated as a JavaBean or not.
If you intend it to be a JavaBean, then Marko Topolnik's answer is accurate.
Otherwise there's no right or wrong. Whether get
or is
(or something else) should be used depends on the contract and purpose of the method. Eran's first comment is spot on:
There's no right or wrong here. I think isMain or hasMain or supportsMain (depnding on what main means) are more descriptive. I don't think it should make a different whether it's boolean or Boolean.
You ask what the convention is, and I'd say that the convention is to name the method as descriptive and semantically accurate as possible.
To elaborate on is vs get:
If the method is intended as a general accessor for a capital-B Boolean
property that make take on the value null
it makes more sense to use get
.
If the method is intended to give the status of an otherwise internal (as in this case private
) non-null flag, I would argue that is
would be an appropriate prefix. (Although I would probably use boolean
as return value, unless there was a common use case to do for instance isMain().hashCode()
or something similar.)
Upvotes: 1
Reputation: 14580
Netbeans is not wrong here - for Boolean
object properties get
is correct. For one thing the property value could be null
, in which case is
wouldn't make sense.
The javabeans spec allows is
for type boolean
as a special case, and does not mention Boolean
. To assume that this special case extends to Boolean
objects would be invalid.
Upvotes: 6
Reputation: 200168
Here is a quote from the actual JavaBeans specification document:
8.3.2 Boolean properties
In addition, for boolean properties, we allow a getter method to match the pattern:
public boolean is<PropertyName>();
This
is<PropertyName>
method may be provided instead of aget<PropertyName>
method, or it may be provided in addition to aget<PropertyName>
method. In either case, if theis<PropertyName>
method is present for a boolean property then we will use theis<PropertyName>
method to read the property value.
Note that this applies to boolean
and not Boolean
values. Also note that is
is an allowed alternative to get
and get
is always appropriate.
Upvotes: 6
Reputation: 103
It is correct . Boolean is a wrapper class of the primitive data type boolean . So Boolean will return object. Same get
is used for object also like primitive data type.
Upvotes: 0
Reputation: 95968
On Boolean
object you can apply many methods: toString
, equals
, valueOf
..
There's no complete answer for your question, it really depends on the usage and who calls the method. It make sense to have:
public boolean isMain(){
return this.main.booleanValue();
}
But if your logic doesn't insure that main
can have null
value, then get
is a good prefix.
Upvotes: 0