Introduction to Java Beans
This article explains basics of Java Beans. JavaBeans is a set of Java Bean classes.
What is a JavaBean?
A Java Bean is a Java class that is mainly responsible for holding onto some data without a large degree of functionality built into the class.
A Java class is said to a Bean class if they have the following properties:
- Has no-arguments constructors.
- Implements the Serializable interface.
- Exposes its properties via getter/setter methods.
JavaBeans Properties
- Its property may be read, write, read-only, or write-only. The JavaBean properties are accessed through two methods, they are: getPropertyName()
- Used to get the the property; for example if we have a property named uname then it should be getUname: setPropertyName()
- Used to set the property; for example if we have a property named uname then
- it should be setUname.
Advantages of JavaBeans
The following are the advantages of using JavaBeans:
- It may be possible to register to receive events from other objects and can generate an event that sent to other objects.
- It provides the ability for properties, events, and methods of a bean to be reused in another application.
- The configuration setting of a bean can be saved in a persistent storage and restored at a later time.
Disadvantages of JavaBeans
The following are the disadvantages of JavaBeans:
- A class with a nullary constructor is subject to being instantiated in an invalid state. If such a class is instantiated manually by a developer (rather than automatically by some kind of framework) then the developer might not realize that the class has been improperly instantiated. The compiler can't detect such a problem, and even if it's documented, there's no guarantee that the developer will see the documentation.
- Having to create a getter for every property and a setter for many, most, or all of them can lead to an immense quantity of boilerplate code.
Java Bean Class Definition
Here is a Java Bean class definition
- public class ClassName implements java.io.Serializable
- {
- /**
- * Property <code>name</code> (note capitalization) readable/writable.
- */
- //Define property-name
- //Getter/Setter method
- }

Join the conversation! Your thoughts help the community grow.