0% found this document useful (0 votes)
143 views

Java Bean

The document discusses JavaBeans, which are reusable software components written in Java. JavaBeans follow certain conventions like having getter and setter methods and implementing serialization. They allow encapsulating objects and accessing their properties. Example code demonstrates creating a basic Employee JavaBean.

Uploaded by

Aarthi E
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
143 views

Java Bean

The document discusses JavaBeans, which are reusable software components written in Java. JavaBeans follow certain conventions like having getter and setter methods and implementing serialization. They allow encapsulating objects and accessing their properties. Example code demonstrates creating a basic Employee JavaBean.

Uploaded by

Aarthi E
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Java Bean

A JavaBean is a software component that has been designed to be reusable in a variety of


environments.

 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.

JavaBeans has several conventions that should be followed:


 Beans should have a default constructor (no arguments)
 Beans should provide getter and setter methods
 A getter method is used to read the value of a readable property
 To update the value, a setter method should be called
 Beans should implement java.io.serializable, as it allows to save, store and restore the state
of a JavaBean you are working on.

JavaBean Features:

1. Introspection – Introspection is a process of analyzing a Bean to determine its capabilities. This is


an essential feature of the Java Beans API because it allows another application such as a design
tool, to obtain information about a component.
2. Properties – A property is a subset of a Bean’s state. The values assigned to the properties
determine the behaviour and appearance of that component. They are set through a setter method
and can be obtained by a getter method.
3. Customization – A customizer can provide a step-by-step guide that the process must follow to
use the component in a specific context.
4. Events – Beans may interact with the EventObject EventListener model.
5. Persistence – Persistence is the ability to save the current state of a Bean, including the values of a
Bean’s properties and instance variables, to nonvolatile storage and to retrieve them at a later time.
6. Methods – A bean should use accessor methods to encapsulate the properties. A bean can
provide other methods for business logic not related to the access to the properties.
What are JavaBean Properties?
A JavaBean property can be accessed by the user of the object. The feature can be of any Java data
type, containing the classes that you define. It may be of the following mode: read, write, read-only,
or write-only. JavaBean features are accessed through two methods:
(i) Accessor
This is the getter method which reads the value. Properties of getter methods are as
follows:
 Must be public in nature
 Return-type should not be void
 The getter method should be prefixed with the word get
 It should not take any argument.

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

The example program shown below demonstrates how to implement JavaBeans.

public class Employee implements java.io.Serializable


{ private int id;
private String name;
public Employee()
{
}
public void setId(int id)
{
this.id = id;
}
public int getId()
{
return id;
}
public void setName(String name)
{
this.name = name;
}
public String getName()
{
return name;
}
}

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

The following list enumerates some of the benefits 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.

Compact and Easy


JavaBeans components are simple to create and easy to use. This is an important focus sector of the
JavaBeans architecture. It doesn’t take much effort to write a simple Bean. Also, a bean is
lightweight, so, it doesn’t have to carry around a lot of inherited baggage to support the Beans
environment.

Carries the Strengths of the Java Platform


JavaBeans is pretty compatible, there isn’t any new complicated mechanism for registering
components with the run-time system

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 life cycle of a JavaBean can be summarized in the following stages:


Instantiation:

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.

You might also like