Hand-Coding with Beans
So far, we’ve seen how to create and use Beans within a Bean application builder environment. That is the primary role of a Java Bean in 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 might use a builder to assemble Beans for the user interface of your application and then load that serialized Bean collection in your own code. 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 these additional
capabilities of JavaBeans we have to use some special tools that take
the place of basic language operations. Specifically, when working
with Beans, we need 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 19.1.
Table 19-1. Methods of the java.beans.Beans Class
|
Operator |
Equivalent |
|---|---|
|
|
|
|
|
|
|
Explicit cast |
|
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. Its advantage over ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access