JavaBeans

JavaBeans are so simple that they might be hard for some people to grasp. I was asked by a college classmate to describe JavaBeans. What I saw in her face was that she did not understand the big fuss over JavaBeans. As much as I tried, she just didn’t get the context and the need for a specific name to such a Plain Old Java Object.

Technically speaking, a JavaBean is a Plain Old Java Object (POJO) which you defined properties for via getters and setters. JavaBeans can be used as third-party components that can be pluggable into a system. Using Java Reflection a system can analyze the available properties for a JavaBean. I have personally used JavaBeans in both InstalledAnywhere and InstalledShield when building custom actions and panels.

To define a JavaBean, the class must be public with the default constructor also publicly available. The constructor is set to public so that when using Reflection you can create an instance without knowing ahead of time the class name, such as:

Class beanClass = Class.forName(classNameStr);
Object bean = beanClass.newInstance();

You define properties by naming convention, using getter and setter methods. For example, for a property named ‘salary’ the JavaBean would define a getSalary and setSalary methods. If the property is a boolean, instead of a get method you define an is method, such as isManager. The parameter type of the set method must be the same as the return type for the get method. It is possible to have a read only property, just provide the getMethod not the setMethod. This is the essentials of JavaBeans but of course you can read the gory details by looking up the JavaBeans Specification.

Related posts:

  1. Jakarta BeanUtils PropertyUtils
  2. Jakarta BeanUtils PropertyUtils Nested Properties
  3. Enterprise JavaBeans 3.0
  4. Simplify Enterprise Development With Scripting
  5. XML Object Cereal
  6. Reopening Ruby Classes


Leave a Reply