I know most variable names will work with "is", such as isBlue()
, but is "has" also a valid prefix, like hasProperty()
?

- 5,429
- 4
- 22
- 31
-
1I've removed the subjective tag as I don't believe it's appropriate (for reasons I've outlined). If the original author could clarify exactly what's required, we can put it back in if it turns out he's not really after the strict validity of JavaBeans naming conventions. – Jon Skeet Apr 28 '09 at 18:59
3 Answers
According to the JavaBeans specification section 8.3.2:
Boolean properties
In addition, for boolean properties, we allow a getter method to match the pattern:
public boolean is<PropertyName>();
This "
isPropertyName
" method may be provided instead of a "get<PropertyName>
" method, or it may be provided in addition to a "get<PropertyName>
" method. In either case, if theis<PropertyName>
method is present for a boolean property then we will use the "is<PropertyName>
" method to read the property value. An example boolean property might be:public boolean isMarsupial(); public void setMarsupial(boolean m);
In other words, unless something has changed since then, has
isn't a valid prefix I'm afraid :(
It's possible that some tools and libraries will recognise such properties anyway, but it's not a good idea to rely on it.

- 5,001
- 4
- 34
- 49

- 1,421,763
- 867
- 9,128
- 9,194
-
1This answer makes perfect sense, but I would say that the question needs to be reworded to mention that the question is specific to the javabeans specification. The tags alone do not convey that information well enough and generally speaking, according the question itself, "has" would be a perfectly acceptable prefix. – Ryan Guill Apr 28 '09 at 18:41
-
1The subject line makes it fairly clear IMO - and the fact that he asks whether a variable name will "work" suggests there's something more than just personal preference involved here. – Jon Skeet Apr 28 '09 at 18:45
-
1For consistency with other property types while still retaining meaningful names I prefer e.g. hasParent and getHasParent() as field and property names respectively, which looking at the cited specification is still perfectly valid. Naming a boolean getter "isX()" isn't always semantically appropriate and I'd prefer verbose code over misleading / confusing code. – Joe Lee-Moyet Jan 20 '15 at 11:08
-
@yjo: I'd use `isChild` in that case :) `getHasParent` feels really ugly to me - it doesn't violate the spec, but it definitely feels wrong. – Jon Skeet Jan 20 '15 at 13:43
-
@JonSkeet sure, it's a (poor) contrived attempt at an example that doesn't need any domain specific context. I'm sure if you thought about it you could come up with some better examples of boolean properties that aren't best expressed with an 'is' verb :). – Joe Lee-Moyet Jan 20 '15 at 13:53
-
@yjo: But the question is whether in each case finding something with `is` might be better than an ugly `getHas` form. My guess is that we'd have different boundaries for that. – Jon Skeet Jan 20 '15 at 13:54
-
@JonSkeet You could argue it's not a 'getHas**X**' form, but a 'get**P**' form where P is a predicate. That way it's consistent with other properties just as I'd prefer 'int numberOfWidgets' over 'int widgets' as the former name gives information that you'd otherwise need to look to the type for. – Joe Lee-Moyet Jan 20 '15 at 13:58
-
@yjo: Again, I'd need to see concrete examples - and again, I suspect we may disagree on the cleanest form. – Jon Skeet Jan 20 '15 at 13:59
-
@yjo, @JonSkeet: How about `isWith` for theses cases (if finding an alternative is difficult)? For example on a person bean you could have a property `withCar` and access it by `isWithCar`. – msa Feb 13 '16 at 10:58
-
@JonSkeet: Does it sound more oddly than `isHasCar` (I'm not a native English speaker)? What would your choice be in this case? – msa Feb 27 '16 at 12:47
-
@msa: I wouldn't use either of those, to be honest. (Not that I'd normally follow Javabeans conventions slavishly unless I needed to.) – Jon Skeet Feb 27 '16 at 13:35
-
With Java 7, I am getting "not a valid getter method for isXXX" method while with getXX it is working. – sandeep kale Jun 24 '16 at 07:39
-
What is correct in the case, where my Bean's boolean property already starts with 'is' like 'isParent' ? is there any solution to create Bean validate getter method for it, without changing the property name? – Parth Vishvajit Jan 05 '17 at 05:28
-
Also watch out for this: `java.beans.Introspector.IS_PREFIX` only works for primitive `boolean` (not `java.lang.Boolean`) as of Java 1.8.0_231. You'll need to use `java.beans.Introspector.GET_PREFIX` for it to work with a `java.lang.Boolean` – akenney Apr 06 '21 at 00:17
Jon Skeet noted that according to the specification it is not valid. Also, canX
, shouldX
, and the likes are not valid. Which is rather unfortunate. Here is a way to check whether a given property has a valid getter:
BeanInfo info = Introspector.getBeanInfo(Item.class);
for (PropertyDescriptor pd : info.getPropertyDescriptors()) {
System.out.println(pd.getName() + " : " + pd.getReadMethod());
}
The class Item
should be a javabean with a foo property, and a getter. If the read method is null
, it means there is no valid getter defined according to the javabeans spec.
This is somewhat subjective, but yes, I would say "has" is a perfectly valid prefix for a Boolean property.
edit the question, as asked, did not mention the javabeans specification and so my answer did not address that aspect of the question. Hence the answer above.

- 13,558
- 4
- 37
- 48
-
9It's not subjective - it's determined by the JavaBeans specification. – Jon Skeet Apr 28 '09 at 18:39
-
1And yet he did not mention that in his question that he was looking for an answer that worked for the javabeans specification. – Ryan Guill Apr 28 '09 at 18:42
-
7
-
I would argue no. I am just suggesting that someone, either you or the original author edit the question to make it more clear. It would also make it more likely to be found using a search engine and make the question more useful. – Ryan Guill Apr 28 '09 at 18:53
-
4@Ryan - There's a reason why He is "Jon Skeet" and we are not :-) – Jose Basilio Apr 28 '09 at 18:54
-
2yes, and I suspect that that fact alone is enough for everyone to agree with him. I was confused by the question because it wasn't clear and can see no reason not to edit the question for clarity. Oh well. – Ryan Guill Apr 28 '09 at 18:57
-
I shall leave that to the author - if you think there is doubt as to whether the question is really talking about "proper" JavaBeans, it would be wrong for me to put the words into the author's mouth. – Jon Skeet Apr 28 '09 at 18:57
-
I agree with @JonSkeet here; the author is clearly referencing the official java beans spec since in the title he says: "JavaBeans" -- the only thing lacking is a "TM" after it! :P – Bane Jul 21 '15 at 21:58