What is a JavaBean?
Author: Deron Eriksson
Description: This Java tutorial describes what a JavaBean is.
Tutorial created using: Windows XP || JDK 1.5.0_09 || Eclipse Web Tools Platform 1.5.1
The term JavaBeanW can be confusing at first, since many people seem to have different definitions of what exactly a JavaBean is. In my usage, a JavaBean is a JavaSW class that is mainly responsible for holding on to some data without a large degree of functionality built into the class.
Typically, a JavaBean is a Java class that:
Implementing the Serializable interface is required if you have a need to save the object (with its data) somewhere (file system, databaseW, refrigerator, etc). It's standard practice to allow the manipulation of the properties within the class via getter/setter methods. Sometimes I may add further methods to a JavaBean class besides the standard getter/setter methods if the nature of particular functionality makes sense to be encapsulated within the bean class, but I make an effort to make such changes the exception to the rule, since this can dirty up your bean classes, which can spread out functionality within your application which can make code maintainability more difficult.
One of the nice features of EclipseSW is the ease with which getter/setter methods can be generated. If we create fields in a Java class, we can generate getter/setter methods by right-clicking and going to Source → Generate Getters and Setters...
We are greeted by the Generate Getters and Setters window, which allows us to select which getters and setters we'd like to automatically generate.
When we click OK, we see that the getters and setters that we selected have been automatically generated for our class. Cool!
This class meets the general definition of a JavaBean mentioned above, since it implements Serializable, has getter/setter methods, and has a no-argument constructor.