Handcoding with Beans
So far, we’ve seen how to create and use beans within a bean application builder environment. That is the primary motivation for JavaBeans, at least in GUI development. But beans are not limited to being used by automated tools. There’s no reason we can’t use beans in handwritten code. You could use a builder to assemble beans for the user interface of your application and then load that serialized bean or a collection of beans in your own code, just as NetBeans does when told to use object serialization. We’ll give an example of that in a moment.
Bean Instantiation and Type Management
Beans are an abstraction over simple Java classes. They
add, by convention, features that are not part of the Java language. To
enable certain additional capabilities of JavaBeans, we use special
tools that take the place of basic language operations. Specifically,
when working with beans, we are provided with replacements for three
basic Java operations: creating an object with new, checking the type
of an object with the instanceof operator,
and casting a type with a cast
expression. In place of these, use the corresponding static methods of
the java.beans.Beans class, shown in
Table 22-1.
Table 22-1. Methods of the java.beans.Beans class
Operator | Equivalent |
|---|---|
| |
| |
Beans.instantiate() is the
new operation for beans. It takes a class loader and the name of a bean class or serialized bean as arguments. ...