22.8. 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. But to run this example, we have to first do a manual build. Select Build Build Main Project, then you can run the file as before with the Run File context menu. The reason for the explicit build is to prompt NetBeans to copy the serialized bean file from the source folder of your source file over to the compiled classes directory (it should be smart enough to do this itself). You should see the serialized bean file, named something like: classes directory). You can run the example and confirm that it behaves exactly like the code-generated version. (This is pretty neat if you think about it.)
We'll discuss working with serialized beans in more detail later in this chapter and ask you to refer to this stored bean file.