JavaBeans was introduced into Java with JDK1.1. In recognition of the fact that
other component models were already in existence, the designers of Java tried to
ensure that JavaBeans components were interoperable with components designed
under these other models. A notable example of this attempt is the ActiveX bridge ,
which can be used to convert a JavaBean into an ActiveX component that can then
be used on a Windows platform. (In addition, Microsoft has opened up the ActiveX
technology to development in a range of languages, including Java.)
An individual JavaBeans component is often referred to as a JavaBean or simply
a 'bean'. Before we look at the construction of JavaBeans, it will help in the under-
standing of what JavaBeans are and how useful they can be by pointing out an
important fact that concerns most of the classes that we have used in our GUI programs:
all Swing and AWT components are JavaBeans . GUI components make ideal
JavaBeans, since they are required in a vast number of applications and would
necessitate an enormous amount of tedious, repetitive coding if they did not exist.
In keeping with the component technology ethos, the application programmer needs
to know nothing of the internal workings of such components. All that he/she
needs to know are what method names and arguments must be supplied and what
values are returned by those methods.
Creating a JavaBean
A bean class has the same basic structure as an ordinary Java class, but with the
particular characteristics listed below.
1. It must be within a named package (and so within a folder of the same name).
2. Each (non-library) public method should begin with either 'get' or 'set'.
3. It does not usually have a main method, but will have if some initial activity is
There are three basic steps in the creation of a bean, as stated below.
1. Write the program code for the required bean functionality.
2. Add any accessor and mutator ('get' and 'set') methods required to allow users
to examine/change properties.
3. Compile the bean and possibly wrap it (and any required resource fi les) in a JAR
(Java Archive) fi le. The latter operation is only really necessary if the bean is
going to be 'fed' into an IDE.
Consideration of 'get' and 'set' methods will be postponed for the time being
while we consider a JavaBean that (initially) makes no use of these methods.
To make things a little more interesting than they might otherwise be, we'll set up a
bean to run an animation that involves the Java mascot 'Duke' juggling some pea-
nuts. A separate thread could be set up to handle the animation, but it is convenient