Introduction to JavaBeans

1. INTRODUCTION TO JAVABEANS

JavaBean technology is a Java-based technology to design and develop reusable and platform- independent software components. These components can then be integrated virtually in all Java- compatible applications. JavaBean components are knows as beans. The appearance and features of a bean can be changed or customized using builder tools such as BeanBox and NetBeans.

A bean class is nothing but a usual Java class with the following requirements:

  • It must have a public no-argument constructor.
  • It has public accessor methods to read and write properties.
  • It should implement Serializable or Externalizable interface.

So, virtually all Java classes are already bean classes or they can be converted to beans with a little effort. For example, the following is an example of a bean class.

//Factorial.java import java.io.*;

public class Factorial implements Serializable {

protected int n;

public int getN() {

return n;

}

public void setN(int n) {

this.n = n;

long prod = 1;

for(int i = 2; i <= n; i++) prod *= i;

fact = prod;

}

protected long fact; public long getFact() {

return fact;

Here is another example of a bean class:

//State.java

import java.io.Serializable;

public class State implements Serializable {

protected String state = “off”; public String getState() {

return state;

}

public void setState(String state) {

this.state = state;

}

}

Most often, JavaBeans have a GUI representation of themselves. For example, button, calculator, and calendar beans are expected to have a visual representation, so as to be useful. However, some beans do not have any visual representation, and are called invisible beans. Invisible beans include a spell checker, random number generator, and temperature monitor.

1.1. Properties

A bean typically has one or more properties that control appearance and behaviour. Properties are discrete, named attributes of a bean. They constitute the data part of a bean’s structure and represent the internal state of a bean. Properties allow us to isolate component state information into discrete pieces that can be easily modified.

In our Factorial bean, there are two properties: n and fact. The property n holds an integer value, whose factorial is stored in the property fact. Here, n is said to be independent property and fact is dependent on n as the value of fact can be calculated from n. There need not be such a relationship in all the cases. For example, in the State bean, there is only one property state. In general, a bean can have any number of properties.

1.2. Accessor Methods

The primary way to access bean properties is through accessor methods. An accessor method is a public method of the bean that directly reads or writes the value of a particular bean. For example, the getN() method of the Factorial bean returns the value of the property n. Similarly, setN() sets the value of n. For the fact property, only one method is provided, which returns the value of the property fact. In general, a property can have two methods associated with it. The methods that are responsible for reading property values are called reader methods. Similarly, methods that are responsible for changing property values are called writer methods.

2. BEAN BUILDER

Readymade applications are available that provide an environment in which we can build and test beans. Those tools are known as builder tools. Some of the popular bean builder tools are Sun’s BeanBox and NetBeans, JBuilder, Borland’s Delphi, etc.

A builder tool is able to display several beans simultaneously. It is also able to connect different beans together in a visual manner. So, we can manipulate a bean without writing any piece of code. Most builder tools have a palette of components from which developers can drag and drop components onto a graphical canvas.

The facilities supported by a bean builder vary widely, but there are some common features as follows:

  • Infrospection—ability to analyze a bean to discover properties, methods, and events
  • Properties—used for customization of the state of a bean
  • Events—a way used by beans to tell the outside that something is happening, or to react to something that happened outside the bean
  • Customization—a developer of a bean can customize the behaviour and appearance of the bean within a builder tool
  • Persistence—a builder tool allows us to customize a bean, and have its state saved away and reloaded later

3. ADVANTAGES OF JAVABEANS

Before we discuss the features and facilities supported by JavaBean technology, let us understand what JavaSoft wanted to accomplish by developing this Java-based component technology. In this regard, we can refer to JavaSoft’s own JavaBeans mission statement: “Write once, run anywhere, use everywhere”. Let us examine each part of this statement to realize what JavaSoft had in mind.

Write Once

A good software component technology should encourage component developers to write code only once. The component should not require rewriting of code to add and/or improve features. Keeping this point in mind, JavaSoft developed JavaBean technology, which allows us to add or improve functionality in the existing code, without re-engineering the original code.

The concept of writing components once also adds sense in terms of version control. This means that developers can make changes to components incrementally, instead of rewriting significant portions from scratch. This results in a steady improvement of functionality, which, in turn, dictates a more consistent software component development through increasing versions.

Run Anywhere

A software technology must be platform independent to have a realistic meaning in today’s rapidly varying software environment. This refers to the capability of software components developed using a technology to be executed (run) in any environment. Fortunately, JavaBeans components are ultimately Java components. Consequently, cross-platform support comes automatically.

Good software components should also run across distributed network environments. This can be simply achieved using Java’s powerful technology, Remote Method Invocation (RMI), Socket, and RMI-IIOP.

Use Everywhere

This is perhaps the most important part of the JavaSoft’s JavaBean mission statement. Well- designed JavaBeans components are capable of being used in a wide range of situations, which include applications, other components, documents, websites, application builder tools, and so on.

In addition to these key features, JavaBean technology has the following built-in features.

JavaBeans are compact components and can be transferred across a low-bandwidth Internet connection that facilitates a reasonable transfer time.

JavaBeans components are so simple that they are not only easy to use, but also easy to develop. Therefore, we can devote more time to embellishing components with interesting features than debugging them.

Source: Uttam Kumar Roy (2015), Advanced Java programming, Oxford University Press.

Leave a Reply

Your email address will not be published. Required fields are marked *