Advance Java

Learn New things from Rixosys

What is JavaBeans?

What is JavaBeans? Introduction to JavaBeans Concepts

Reusability is the main concept in any programming language. A JavaBean is a software component that has been designed to be reusable in a variety of environments. Let us dive into the topic and understand the horizons of concept in this ‘What is JavaBeans’ article.

Following pointers will be our topics of discussion:

Let’s begin!

What is JavaBeans?

JavaBeans is a portable, platform-independent model written in Java Programming Language. Its components are referred to as beans. 

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
    • 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

Now that you are familiar with basics, let’s learn in detail about the properties of JavaBeans.

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:

 

1. getEmployeeName ()

For example, if the employee name is firstName, the method name would be getFirstName() to read that employee name. This method is known as an accessor. Properties of getter methods are as follows:

  1. Must be public in nature
  2. Return-type should not be void
  3. The getter method should be prefixed with the word get
  4. It should not take any argument

2.setEmployeeName ()

For example, if the employee name is firstName, the method name would be setFirstName() to write that employee name. This method is known as a mutator. Properties of setter methods:

  1. Must be public in nature
  2. Return-type should be void
  3. The setter method has to be prefixed with the word set
  4. It should take some argument

Now that you have attained some theoretical knowledge about JavaBeans, let us move on and understand the implementation process.

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("Rixosys"); 
    System.out.println(s.getName()); 
}
}

Output:

Rixosys

So, that’s how to implement a Java program which accesses the JavaBean class.

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.

Though all these sound good, using JavaBeans presents some disadvantages as well. Now, let’s check out what those would be.

Disadvantages of JavaBeans

  1. JavaBeans are mutable, hence lack the advantages offered by immutable objects.
  2. JavaBeans will be in inconsistent state partway through its construction.