Java Bean
Java Bean
What is JavaBeans?
What are the Properties of JavaBean?
Example Program: Implementation of JavaBeans
Advantages of JavaBeans
Disadvantages of JavaBeans
What is JavaBeans?
JavaBeans is a portable, platform-independent model written in Java Programming Language. Its
components are referred to as beans. A bean encapsulates many objects into one object so that we
can access this object from multiple places. Moreover, it provides easy maintenance.
In simple terms, JavaBeans are classes which encapsulate several objects into a single object. It helps
in accessing these object from multiple places. JavaBeans contains several elements like
Constructors, Getter/Setter Methods and much more.
JavaBean Features:
For example, if the employee name is firstName, the method name would be getFirstName() to
read that employee name.
(ii) Mutator
This is the setter method to write or update value. Properties of the mutator are
Must be public in nature
Return-type should be void
The setter method has to be prefixed with the word set
It should take some argument
For example, if the employee name is firstName, the method name would be
setFirstName() to write that employee name.
Example Program: Implementation of JavaBeans
Next program is written in order to access the JavaBean class that we created above:
public class Employee1 {
public static void main(String args[])
{
Employee s = new Employee();
s.setName("Chandler");
System.out.println(s.getName());
}
}
Output:
Chandler
Advantages of JavaBeans
Portable
JavaBeans components are built purely in Java, hence are fully portable to any platform that
supports the Java Run-Time Environment. All platform specifics, as well as support for JavaBeans, are
implemented by the Java Virtual Machine.
Disadvantages of JavaBeans
JavaBeans are mutable, hence lack the advantages offered by immutable objects.
JavaBeans will be in inconsistent state partway through its construction.
Life Cycle of Java Beans
The life cycle of a JavaBean refers to the different stages that a JavaBean instance goes through,
from its creation to its destruction.
The JavaBean is created by invoking its constructor. At this stage, memory is allocated for the
JavaBean instance, and any initialization code within the constructor is executed.
Initialization:
After the JavaBean is instantiated, initialization logic is performed. This may involve setting default
property values, establishing connections to resources, or performing any other necessary setup
tasks. Initialization can be done within the constructor or in a separate initialization method.
Property Setting:
Once the JavaBean is initialized, its properties can be set using setter methods or other means.
Properties represent the state of the JavaBean and can be modified as needed.
Active Use:
The JavaBean is actively used, and its methods are invoked to perform desired operations. The
JavaBean interacts with other components or systems, responds to events, and carries out its
designated functionality.
Passive Use:
The JavaBean may enter a passive state where it is not actively used or interacted with. However, it
remains in memory and retains its property values.
Destruction:
At some point, the JavaBean may be destroyed, either explicitly or implicitly. Explicit destruction can
be triggered by invoking a specific method, while implicit destruction occurs when the JavaBean
goes out of scope or when the application or container shuts down. Destruction typically involves
releasing resources, closing connections, and performing any necessary cleanup tasks.