| class Astronaut { |
| |
» | String name; |
| boolean retired; |
| |
» | Astronaut(String name) { |
| this.name = name; |
| } |
| |
| String getFullName() { |
| return name; |
| } |
| |
| void setFullName(String name) { |
| this.name = name; |
| } |
| |
» | boolean getRetired() { |
| return retired; |
| } |
| |
» | void setRetiredState(boolean retired) { |
| this.retired = retired; |
| } |
| } |
In object-oriented programming languages, you usually try to avoid direct access to the fields of a class from the outside. That’s why you write getter and setter methods that control this access.
The structure and naming of getters and setters is so standardized that many frameworks heavily rely on it. Hibernate uses it to convert Java instances and rows in a SQL database, Jackson uses it for JSON messages, and even the Play Framework uses it for building HTML forms. That’s why getters and setters have a specification of their own, the JavaBeans specification.[30] We call classes that follow that convention JavaBeans and yes, it somehow stretches that coffee metaphor of Java.
Take a look at the code above. Does every field have a correctly named getter and setter? Is there a constructor without any parameters? Are the visibility levels correct?
No, they’re not. Strange things can happen when you use instances of that class within frameworks. Most frameworks won’t throw exceptions but will fail silently—or simply work in ways you didn’t expect.
So what do we need to change to turn this class into a JavaBean?
| class Astronaut { |
» | private String name; |
| private boolean retired; |
| |
» | public Astronaut() { |
| } |
| |
| public Astronaut(String name) { |
| this.name = name; |
| } |
| |
| public String getName() { |
| return name; |
| } |
| |
| public void setName(String name) { |
| this.name = name; |
| } |
| |
» | public boolean isRetired() { |
| return retired; |
| } |
| |
» | public void setRetired(boolean retired) { |
| this.retired = retired; |
| } |
| |
| } |
We’ve reworked the code in a number of ways. Now the class above is a valid JavaBean. First, we changed the modifiers of the fields to private and the getters and setters to public.[31] This ensures that frameworks will use the getters and setters and won’t circumvent them by accessing fields directly.
Next, we added a default constructor. Most frameworks need this to create a blank instance of a class, which they configure by calling setters to assign values to fields. You can easily overlook this part because the default constructor isn’t available anymore if you add another one with parameters.
And last, we renamed the setters and getters to reflect the name of the field. If the field has the name foo then the getter and setter should be named getFoo() and setFoo(). Be aware that this changes slightly for a boolean field. The name of the setter stays the same, but the getter turns to isFoo() instead—it reads like a question. Too bad Java doesn’t allow us to put a question mark there.
One last note: We’re not saying that JavaBeans are a great way of writing code, but you’ll have to use them for some frameworks in Java.
3.137.184.102