Serialization Versus Code Generation
If you’ve been keeping an eye on the NetBeans source window
while we’ve been working, you may have noticed the code that is being
generated when you modify properties of beans. By default, NetBeans
generates method calls to set values on beans in the initComponents() method.
For example, if you set the value of one of your NumericField beans to 42 in the Properties pane, this value gets
hardcoded into the application as an initial value of the bean by a call
to setValue() on the bean
object at initialization time. But if you click on the Code button in the
Properties pane, you’ll see that we have another option. This area holds
properties that govern how NetBeans generates the application code. By
changing the Code Generation property from Generate Code to Serialize, you
change NetBeans’ behavior. Instead of generating method calls in the
source code, it saves your fully configured bean as a serialized object
and then generates the appropriate code to load the freeze-dried bean into
the application from a file.
Try changing the code generation property for a Juggler bean to Serialize. Switching to the
source code view and looking at the initComponents() method, you’ll see a line for
that bean that uses the static Beans.instantiate() method to load a serialized
copy of the bean.
NetBeans treats the serialized bean file as part of the source code and will regenerate it whenever it saves the source file. In order to run this example, we must first ...