JavaBeans
In computing based on the Java Platform, JavaBeans are classes that encapsulate many objects into a single object. They are serializable, have a zero-argument constructor, and allow access to properties using getter and setter methods. The name "Bean" was given to encompass this standard, which aims to create reusable software components for Java.
It is a reusable software component written in Java that can be manipulated visually in an application builder tool.
Features
;Introspection;Properties
;Customization
;Events
;Persistence
;Methods
Advantages
- The properties, events, and methods of a bean can be exposed to another application.
- A bean may register to receive events from other objects and can generate events that are sent to those other objects.
- Auxiliary software can be provided to help configure a bean.
- The configuration settings of a bean can be saved to persistent storage and restored.
Disadvantages
- A class with a zero-argument constructor is subject to being instantiated in an invalid state. If such a class is instantiated manually by a developer, the developer might not realize that the class has been improperly instantiated. The compiler cannot detect such a problem, and even if it is documented, there is no guarantee that the developer will see the documentation.
- JavaBeans are inherently mutable and so lack the advantages offered by immutable objects.
- Having to create getters for every property and setters for many, most, or all of them can lead to an immense quantity of boilerplate code. This can be mitigated using tools like .
JavaBeans API
Interface | Description |
AppletInitializer | Methods in this interface are used to initialize Beans that are also applets. |
BeanInfo | This interface allows the designer to specify information about the events, methods and properties of a Bean. |
Customizer | This interface allows the designer to provide a graphical user interface through which a bean may be configured. |
DesignMode | Methods in this interface determine if a bean is executing in design mode. |
ExceptionListener | A method in this interface is invoked when an exception has occurred. |
PropertyChangeListener | A method in this interface is invoked when a bound property is changed. |
PropertyEditor | Objects that implement this interface allow the designer to change and display property values. |
VetoableChangeListener | A method in this interface is invoked when a Constrained property is changed. |
Visibility | Methods in this interface allow a bean to execute in environments where the GUI is not available. |
JavaBean conventions
In order to function as a JavaBean class, an object class must obey certain conventions about method naming, construction, and behaviour. These conventions make it possible to have tools that can use, reuse, replace, and connect Java Beans.The required conventions are as follows:
- The class must have a public default constructor. This allows easy instantiation within editing and activation frameworks.
- The class properties must be accessible using get, set, is, to and other methods according to a standard naming convention. This allows easy automated inspection and updating of bean state within frameworks, many of which include custom editors for various types of properties. Setters can have one or more than one argument.
- The class should be serializable.
Code example
package player;
public class PersonBean implements java.io.Serializable
TestPersonBean.java
:import player.PersonBean;
/**
* Class "TestPersonBean".
*/
public class TestPersonBean
Name:
Deceased?