A walking tour of JavaBean

Software components are to software what integrated circuits (ICs) are to electronics: "black boxes" that encapsulate functionality and provide services based on a specification. Software reusability has long been a holy grail of software engineering and a major goal of object-oriented programming. Software components are designed to be reusable, even interchangeable.

JavaBeans are reusable software components for Java that can be manipulated visually in a builder tool.Practically, they are classes written in the Java programming language conforming to a particular convention. They are used to encapsulate many objects into a single object (the bean), so that the bean can be passed around rather than the individual objects.

Java Bean

A Java Bean is a java class that should follow following conventions:
  • It should have a no-arg constructor.
  • It should be Serializable.
  • It should provide methods to set and get the values of the properties, known as getter and setter methods.


In computer science, in the context of data storage, serialization is the process of translating data structures or object state into a format that can be stored (for example, in a file or memory buffer) or transmitted (for example, across a network connection link) and reconstructed later (possibly in a different computer environment)

Serializable Objects

To serialize an object means to convert its state to a byte stream so that the byte stream can be reverted back into a copy of the object. A Java object is serializable if its class or any of its superclasses implements either the java.io.Serializable interface or its subinterface, java.io.Externalizable. Deserialization is the process of converting the serialized form of an object back into a copy of the object.

Why use Java Bean?

According to Java white paper, it is a reusable software component. A bean encapsulates many objects into one object, so we can access this object from multiple places. Moreover, it provides the easy maintenance.

JavaBeans Properties

A JavaBean property is a named attribute that can be accessed by the user of the object. The attribute can be of any Java data type, including the classes that you define.

A JavaBean property may be read, write, read only, or write only. JavaBean properties are accessed through two methods in the JavaBean's implementation class −

S.No.Method & Description
For example, if property name is firstName, your method name would be getFirstName() to read that property. This method is called accessor.
For example, if property name is firstName, your method name would be setFirstName() to write that property. This method is called mutator.
A read-only attribute will have only a getPropertyName() method, and a write-only attribute will have only a setPropertyName() method.

JavaBeans Example

Consider a student class with few properties −

package com.SuvenConsultants;

public class StudentsBean implements java.io.Serializable {
   private String firstName = null;
   private String lastName = null;
   private int age = 0;

   public StudentsBean() {
   public String getFirstName(){
      return firstName;
   public String getLastName(){
      return lastName;
   public int getAge(){
      return age;
   public void setFirstName(String firstName){
      this.firstName = firstName;
   public void setLastName(String lastName){
      this.lastName = lastName;
   public void setAge(Integer age){
      this.age = age;
How to Access Java Bean Class

package com.SuvenConsultants;
public class Suven{  
public static void main(String args[]){  
StudentsBean sc=new StudentsBean ();//object is created  
sc.seFirsttName("Rocky");//setting value to the object  
The useBean action declares a JavaBean for use in a JSP. Once declared, the bean becomes a scripting variable that can be accessed by both scripting elements and other custom tags used in the JSP. The full syntax for the useBean tag is as follows:
<jsp:useBean id = "bean's name" scope = "bean's scope" typeSpec/>

Here values for the scope attribute can be a page, request, session or application based on your requirement. The value of the id attribute may be any value as a long as it is a unique name among other useBean declarations in the same JSP.

Following example shows how to use the useBean action −
      <title>useBean Example</title>
      <jsp:useBean id = "date" class = "java.util.Date" /> 
      <p>The date/time is <%= date %>
You will receive the following result − −
The date/time is Thu Sep 30 11:18:11 GST 2017 

Accessing JavaBeans Properties

Along with action, you can use the action to access the get methods and the action to access the set methods. Here is the full syntax −

<jsp:useBean id = "id" class = "bean's class" scope = "bean's scope">
   <jsp:setProperty name = "bean's id" property = "property name"  
      value = "value"/>
   <jsp:getProperty name = "bean's id" property = "property name"/>
The name attribute references the id of a JavaBean previously introduced to the JSP by the useBean action. The property attribute is the name of the get or the set methods that should be invoked.
Following example shows how to access the data using the above syntax −

      <title>get and set properties Example</title>
      <jsp:useBean id = "students" class = "com.SuvenConsultants.StudentsBean"> 
         <jsp:setProperty name = "students" property = "firstName" value = "Rocky"/>
         <jsp:setProperty name = "students" property = "lastName" value = "Jagtiani"/>
         <jsp:setProperty name = "students" property = "age" value = "39"/>

      <p>Student First Name: 
         <jsp:getProperty name = "students" property = "firstName"/>
      <p>Student Last Name: 
         <jsp:getProperty name = "students" property = "lastName"/>
      <p>Student Age: 
         <jsp:getProperty name = "students" property = "age"/>

The following result will be displayed −

Student First Name: Rocky

Student Last Name: Jagtiani

Student Age: 39

Benefits of JavaBeans:

  • A Bean obtains all the benefits of Java's "write-once, run-anywhere" paradigm. 
  •  The properties, events, and methods of a Bean that are exposed to an application builder tool can be controlled. 
  • A Bean may be designed to operate correctly in different locales, which makes it useful in global markets.
  •  Auxiliary software can be provided to help a person configure a Bean. This software is only needed when the design-time parameters for that component are being set. It does not need to be included in the run-time environment.
  •  The configuration settings of a Bean can be saved in persistent storage and restored at a later time. 
  • A Bean may register to receive events from other objects and can generate events that are sent to other objects.

Want To Learn Java?

Popular posts from this blog

MongoDB - daTa ModeLLinG Concepts: SQL - Where all to UsE ?

Comparing Bootstrap With Google's Material Design Lite : Bootstrap - ResponSive Design

MongoDB overView with Installation_procEss: SQL - Where all to UsE ?