Java Reference
In-Depth Information
Let me make a few comments about the Movie class:
First, the rentMovie() and returnMovie() methods are simply methods
in the class and do not define any properties of this bean.
■■
The class implements java.io.Serializable and contains a no-argument
constructor, both features of all JavaBeans.
■■
The bean has two read-write properties: title, which is a String, and
length, which is an int.
■■
The bean has a write-only property named customerName of type
String.
■■
The bean has a read-only property named rented of type boolean.
■■
The Movie class has four fields, but they have nothing to do with bean
property names or data types. In fact, bean builder tools are not aware
of these fields because they are private.
■■
Now that you have seen a bean (the Movie class) and how to create simple
properties for a bean, I will show you how to package the bean in a JAR file
and use the bean in a builder tool.
Packaging a Bean
JavaBeans are accessed by builder tools in the following formats:
JAR files. The bean class and any other utility classes that the bean needs
are packaged in a JAR file. I will show you this technique first.
Serialized files. The bean to be accessed is a serialized object in a file
whose extension is .ser. The file was created using the java.io.ObjectOut-
putStream class.
XML archive. The bean was serialized using the java.beans.XMLEncoder
class. This is the preferred technique to serialize a bean, and it is also a
new technique as of J2SE 1.4.
Serializing beans is a common aspect of JavaBeans, and this was done
using the standard Java serialization up until J2SE 1.4. However, with XML
becoming widely used in all aspects of Java programming, the new
XMLEncoder and XMLDecoder classes offer an alternate way to persist
beans. See the upcoming sidebar Bean Persistence .
Search WWH ::




Custom Search