Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why isn't "has"... the beginning of a valid JavaBean method signature? [closed]

Tags:

java

javabeans

The JavaBeans methods' signature's has to follow certain conventions such as set.../get... and such. They have a convention for is...for instance isEven() could be a method for an Integer class to test a boolean. But then I wonder why not has... is also a legal identifier since it makes sense to me to test what something has e.g. hasCar() for a Person class or likewise.

Do you understand my question? What do you think?

like image 926
Niklas Rosencrantz Avatar asked Aug 13 '12 13:08

Niklas Rosencrantz


People also ask

Which of the following is a valid JavaBean method signature?

Options B, C, and F are each proper JavaBean method signatures.

What are the methods correct for JavaBean?

To access the JavaBean class, we should use getter and setter methods.

What are the characteristics of a JavaBean?

JavaBeans provide default constructor without any conditions or arguments. JavaBeans are serializable and are capable of implementing the Serializable interface. JavaBeans usually have several 'getter' and 'setter' methods. JavaBeans can have several properties that can be read or written.

What is the purpose of a JavaBean?

JavaBeans is a portable, platform-independent model written in Java Programming Language. Its components are referred to as beans. In simple terms, JavaBeans are classes which encapsulate several objects into a single object. It helps in accessing these object from multiple places.


2 Answers

Well, the general convention is to use get... and set... and thus is... is just an exception for booleans. For is... the convention is easy: you need to return a boolean, can skip the getter and the corresponding setter will take a boolean parameter as well.

A convention for has... would be more difficult: has... would return a boolean but you'd still need a getter and setter which deal with different types. Thus has... is no replacement for get... as opposed to is... and since that part of the JavaBeans convention normally only is about getters and setters has... doesn't fit in there.

From the JavaBeans spec:

Properties are discrete, named attributes of a Java Bean...

Properties show up in a number of ways:

  1. ...
  2. Properties can be accessed programmatically by other components calling their getter and setter methods (see Section 7.1 below).
  3. ...

Any property being accessed using has... would not be persistent nor be accessed by its getter method.

Example: if a person has a car property, you'd expect to have a getCar() accessor. hasCar() wouldn't be an accessor since the derived property hasCar would need an accessor named getHasCar() or isHasCar(). If has was an accessor prefix, the property would have the conflicting name car.

like image 52
Thomas Avatar answered Oct 24 '22 19:10

Thomas


get/set and is/set for boolean type is just the convention. I agree that sometimes it is more readable to call getter hasSomething. For example hasChildren(). But let's go forward and say that it is fine to call method canWrite() or willFly() or providesResult() etc. And you can use such names but these names are not following java bean convention and therefore will not be recognized as getters by java bean frameworks.

Java Beans were invented a lot of years ago. Probably it is not a bad idea to define annotations @Setter and @Getter. If these annotations become common all java bean frameworks could support them and programmers will be able to call getters as they want but mark them with annotation @Getter. But such annotations are not supported now, so the only advise is to follow existing naming conventions.

like image 43
AlexR Avatar answered Oct 24 '22 20:10

AlexR