Developing Java Beans throws java.io.IOException; If the readObject()

Motilium Online Buy Acomplia Acomplia Online Buy Prednisone Motilium No Prescription Antabuse For Sale Elimite Generic Buy Nexium Online Propecia Without Prescription Nizoral No Prescription

Developing Java Beans throws java.io.IOException; If the readObject() method exists, the default serialization of data members will not take place. Again, it is up to the readObject() method to reconstruct the state of the object. You should always call defaultReadObject() on the stream before reading back any additional data. Just like before, this method implements the default serialization of the non-static and non-transient data members of the object being reconstructed. The defaultReadObject() method implements the logic necessary to deal with class evolution, which is discussed later. You should also note that the defaultReadObject() method can throw a ClassNotFoundException. This will happen if the class of the object being restored is not available. Remember that the object may be restored on a different machine than the one that saved it, and that machine may have a very different set of classes available. Let’s make the WatchList class from the previous chapter serializable. We do this by declaring that the class implements java.io.Serializable. The class defines one data member, an instance of java.util.Vector, which is named stocks. The class java.util.Vector is itself serializable, and we are using it to store instances of java.lang.String, which is also serializable. The only required change to the WatchList class then is to declare that it implements the java.io.Serializable interface, as shown here: public class WatchList implements java.io.Serializable { // a vector that contains the actual stock names protected Vector stocks = new Vector(); [ the rest of the WatchList code goes here ] } The same thing holds true for the Temperature, Thermometer, and Thermostat classes introduced in previous chapters. The only change required to allow these classes to participate in object serialization is to declare that each implements the java.io.Serializable interface. This is not to say that every subclass of a serializable class will itself be serializable. Implementing java.io.Serializable is only one part of the process. As we’ll see shortly, it may require some effort to deal with the serialization of data members that are not themselves serializable. 5.4 Walking the Class Hierarchy When an object is serialized, the highest serializable class in its derivation hierarchy is located and serialized first. Then the hierarchy is walked, with each subclass being serialized in turn. In order to illustrate this, let’s create an example with a three-level class hierarchy. Class C is a subclass of class B, which is a subclass of class A. Since class A implements java.io.Serializable, all of its subclasses are considered serializable as well. Again, this is due to the fact that all of its data members can be serialized. Each class implements the writeObject() and readObject() methods. class A implements java.io.Serializable{ protected int a; private void writeObject(ObjectOutputStream stream) throws java.io.IOException { System.out.println(”writeObject called for class A”); stream.defaultWriteObject(); } page 79

Comments are closed.