Java Bean Programming

Introduction

The primary idea behind developing the JavaBean class is to encapsulate many objects under one single object or bean. As Oracle describes it, ‘The JavaBean component API extends the Java platform’s Write Once, Run Anywhere capability to reusable component development.’ JavaBeans lays a lot of stress in the interoperability of its code. This is also a key feature of the Java Beans architecture. The program you write in JavaBeans holds the capability to run on any Operating System and within any application environment or framework. Some of the applications and services the Beans interoperate with are ActiveX, OpenDoc, and LiveConnect.

All Java Bean classes should follow the following conventions:

  • Must be Serializable
  • Must have a public no-arg constructor
  • All Java bean class properties must be private. Only the setter and getter methods to set and get values of the properties can be public.

Java Bean properties

Every class in Java Bean has a set of properties. The properties can be of any datatype and type namely read, write, read-only, or write-only. There are two specified methods through which we can interact with the property values.

  • getPropertyName() – The method name will change depending on the name of the property. For example, if the property is rollNumber, then the method name will change to getRollNumber(). This is an accessor method.
  • setPropertyName() – This is the mutator method that will fetch the value of the property. Considering the earlier method, setRollNumber() will return the roll number of the person concerned.

Advantages of Java Bean

  • Due to the feature of interoperability, all Java Bean classes and properties can be used on any other environment or operating system. Therefore, Java Bean gives you universal use.
  • All of the software components can be reused.

Disadvantages of Java Beans

  • Immutable objects are rendered useless as Javabeans are mutable.
  • A lot of repetitive code is required to set and get property values of all the properties of Java Beans.

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.