Reputation:
Simple question but I can't understand.
Example with is
prefix:
private boolean active;
public boolean isActive(){...};//getter
public boolean setActive(boolean b){...};//setter
But I don't understand how to do setter with has
prefix
private boolean hasChildren;
public boolean hasChildren(){...};//getter
public boolean ?(boolean b){...}//setter
Upvotes: 7
Views: 7530
Reputation: 54649
The problem in your case is not only finding an appropriate name for the setter. It is also that your getter does not follow the conventions!
The specification and the tutorial are very clear on this:
set
get
is
And it is important to follow these conventions, because otherwise, many automatic (reflection-based) tools will no longer work as expected (e.g. Introspectors).
Grammar aside, there are several options for solving this. In a comment, it was suggested to call the property isParent
, although then, strictly speaking, the accessor would have to be called isIsParent
...
So I'd suggest to simply call the property something like havingChildren
or owningChildren
, and offer the corresponding accessor methods like isHavingChildren
/setHavingChildren
, or
isOwningChildren
/setOwningChildren
, respectively.
Upvotes: 12
Reputation: 5074
According to the JavaBeans Specification (section 8.3.2) accessor methods for a boolean field foo
are isFoo()
and setFoo(boolean)
accordingly.
Your case would be a accessor pair isHasChildren()
/setHasChildren()
.
As a suggestion, your should consider renaming of the field into boolean childrenPresent;
, which would, according to the spec, be derived into isChildrenPresent()
and setChildrenPresent()
.
Upvotes: 3
Reputation: 25960
You can safely go for setHasChildren
as it is totally clear. Nevertheless if you are storing more information about the children, it could change the answer.
For example, if you need to store the children, only the setter would make sense and the children would be represented by a collection :
// no hasChildren attribute
private Collection<ChildClass> children;
public boolean hasChildren(){ return !children.isEmpty(); };
// no setter
Another example, if you are storing the number of children :
// no hasChildren attribute
private int childrenCount;
public boolean hasChildren(){ return childrenCount > 0 };
// no setter
Upvotes: 0
Reputation: 1074485
If it's really as in your code, where you have a boolean
for this, go ahead and use setHasChildren
.
Usually, "has" sorts of attributes don't have simple setters because they're usually reporting a state that isn't backed by a simple boolean, e.g.:
private List<Child> children;
// ...
public boolean hasChildren() {
return children.size() > 0;
}
// ...
public void addChild(Child c) {
// ...
}
But when that's not the case, setHasXyz
is fine.
Upvotes: 0
Reputation: 6359
hasChildren is inspecting the state of an object and as such I wouldn't usually expect a setter method.
I would expect that if you had no children (hasChildren() == false) and then were to addChild(Whatever) then the next time you call hasChildren, it would return true.
By attempting to manipulate that behaviour manually, you're breaking encapsulation and the Law of Demeter as your calling program should know about its owned objects, but not the implementation of its objects own objects. I hope that makes some sense.
To make a crude analogy, you could ask a lady if she's with child and she will (hopefully) report her status (or start crying because you just called her "large). If false, you could proceed to make her pregnant (subject to consent of course), you wouldn't just cram a baby up there and say "there, you're pregnant now".
Upvotes: 0
Reputation: 7730
For Boolean you can use is
as a prefix in getter , or you can also use
get
as a prefix
public boolean isHasChildern();//getter
public boolean getHasChilder();//getter
public boolean setHasChildren(boolean active);//setter
Upvotes: 0
Reputation: 267
When we are talking about hasChildren , hasAttributes , we are talking about whether there is any collections attached to it. Like children can be many , eg can be a list or a map whereas incase of isActive we are just denoting a single property value. You can set the value setHasChildren. But then I think naming convention is for convenience of understanding.
Upvotes: -1