Java: Java Beans – An Introduction

Basic JavaBeans are nothing but POJOs. Before discussing about JavaBeans; let’s have a quick look at POJOs.

POJO (Plain Old Java Object) is a term used to indicate that it is not a special Java object (not developed using any framework or any other technology); rather it is an ordinary plain simple Java object. The term POJO was first introduced by Martin Fowler.

In Java, if a class is said to be a POJO, it has to satisfy the following conditions:

  • It should not transcend JLS (Java Language Specifications).
  • It should not extended from any framework provided classes but POJO can extend our own user defined classes and standard Java classes.
  • It should not implement any predefined interfaces.
  • It should not adorn with any annotations that suppress POJO functionality.

What advantages we will get, if we use POJOs?

  • We can achieve loose coupling.
  • We can easily migrate from one framework to other.
  • The POJO programming model enables to unit test outside of the application server; making the whole programming experience smoother.
  • We can achieve clean separation of business logic.

Let’s take a simple POJO example.

// POJOExample.java
package com.codesteps.bean;

public class POJOExample
{
    private String name;

    public String getName()
    {
        return name;
    }

    public void setName(String name)
    {
        this.name = name;
    }
}

This simple example has a property “name”, getter and setter methods to deal with the property. It is a simple POJO class; but it followed a proper naming convention which is required for creating JavaBeans. So, this class is a POJO class as well as a simple JavaBean.

Let’s start using this simple JavaBean.

Step 1. Compile our JavaBean example.

javac POJOExample.java

Step 2. After successful compilation our JavaBean is ready to use. Then, how to use our JavaBean? Observe that our JavaBean doesn’t have a main() method. So, we can not use this as a standard application. What we do is, we will write a simple Java test example and use our JavaBean from the test example.

Let’s write a simple test example to use our POJOExample. The code looks like below:

[Malin Oct-23-2014: Provide here a simple test example.]

Step 3. Now compile and run our test example. Observe that our JavaBean is working successfully.

Is that what we expect from a JavaBean? Observe that, our JavaBean is just like a normal Java program with a simple Java class. We have used this simple Java class from another Java program. That’s it.

I know, now you will ask one question. Is this is the purpose of JavaBean? The answer is, no, it is not the exact purpose of introducing JavaBeans. So, what is the purpose of introducing JavaBean? Good. Let’s discuss more about our JavaBeans to figure out the answer to this question.

Java Beans are reusable software components. That means we can use Java Beans in other components or in other applications. Java Beans can be used in small applications to very large applications. Also a Java Bean can run as an individual component or a distributed component.

As discussed, Java Beans can interact with other Java Beans or other applications or tools. Because of this nature, we need to follow some guidelines while writing code for Java Beans. Because of this, there must be a mechanism to expose Java Beans’ information (properties, methods and events) to the outside world.

Java Beans are interactive or non-interactive. That means, Java Beans are visible to the user to interact with the users. Or invisible to the users to do some background processing. For example: the button on the window may be a Java Bean; when the user clicks on the button, it may interact with the invisible Java Bean to process the user request.

Java Bean is also a POJO with an additional feature is that, Java Bean must implement java.io.Serializable interface.

There is a syntactic difference between a Java Bean and a regular java class. If we want to say any java class as a Java Bean it has to satisfy all the following conditions:

  1. The java class must implement java.io.Serializable interface or it’s any child interface. Because only serializable objects can be written to network streams, file streams, database etc..
  2. Java class must have a public no argument constructor.
  3. All the class private properties must have getter and setter methods.

Today in the real world almost all frameworks uses Java Beans to write their business logic and configure the beans in their corresponding configuration files then transmit over the network.

As we discussed above, the Java Beans class must implement Serializable interface. So now the question is, what will happen when it is not implement a Serializable interface.? The answer is, nothing will happen even you don’t extend it from Serializable interface; no errors will show, when you compile the program. But remember that, the Java Bean can’t transfer over the network. As per the Java Bean standard every Java Bean must able to transfer over the network; since the business related data is stored in Bean objects. So it needs to exchange between multiple systems.

Now it is the time to have a look at simple Java Bean. Below is an example:

package com.codesteps.bean;
import java.io.Serializable;

public class BeanExample implements Serializable
{
    public BeanExample()
    {
       //default constructor
    }
}

The above example is a simple Java Bean. The above code is simple to understand. Hence not giving code explanation here.

Thank You.

Leave a Reply