Java Reference
In-Depth Information
numbers[0] = 0.6765377168813207
numbers[1] = 0.3933764846876555
numbers[2] = 0.7633265658906377
numbers[3] = 0.31411955819992887
You should get something vaguely similar.
How It Works
We first create three objects of type Junk in the main() method. We then define a variable that will
hold the stream reference. If we were to define this variable within the first try block, then it would not
exist beyond the end of the try block so we could not refer to it after that point. Within the try block
we create the ObjectOutputStream object that we will use to write objects to the file
C:/Beg Java Stuff/JunkObjects.bin , via a buffered output stream. Each Junk object is written
to the file by passing it to the writeObject() method for the ObjectOutputStream object. Each
object will be written to the file including the values of its three instance fields, answer , thought , and
numbers . The String object and the array are written to the file as objects. This is taken care of
automatically and requires no special provision within our code. The static field, generator , is not
written to the file.
Before we exit the program we close the stream by calling its close() method. We could put the call
to close() within the first try block, but if an exception was thrown due to an I/O error the method
would not get called. By putting it in a separate try block we ensure that we do call the close()
method. The stream would be closed automatically when the program terminates but it is good practice
to close any streams as soon as you are done with them. We will read the objects back from the file a
little later in this chapter.
Conditions for Serialization
In general there can be a small fly in the ointment. For implementing the Serializable interface, to
be sufficient to make objects of the class serializable, all the fields in the class must be serializable (or
transient - which we will come to), and all superclasses of the class must also be serializable. This
implies that the fields must be either of primitive types or of class types that are themselves serializable.
If a superclass of your class is not serializable, it still may be possible to make your class serializable.
The conditions that must be met for this to be feasible are:
Each superclass that is not serializable must have a public default constructor - a constructor
with no parameters.
Your class must be declared as implementing the Serializable interface.
Your class must take responsibility for serializing and deserializing the fields for the
superclasses that are not serializable.
This will usually be the case for your own classes, but there are one or two classes that come along with
Java that do not implement the Serializable interface, and what's more, you can't make them
serializable because they do not have a public default constructor. The Graphics class in the package
java.awt is an example of such a class - we will see more of this class when we get into programming
using windows. All is not lost however. There is an escape route. As long as you know how to
reconstruct any fields that were not serializable when you read an object back from a stream, you can
still serialize your objects by declaring the non-serializable fields as transient.
Search WWH ::




Custom Search