Developing Java Beans The next applet, SimpleReader, simply reads the serialized button object from file Saver.tmp and adds it to the applet. When this applet is run, the button appears with the properties that were established in the first example. You’ll have to take it for granted that this applet creates the same display (Figure 5.2) as the previous one, SimpleSaver. The code for SimpleReader is shown next: import java.applet.*; import java.awt.*; import java.io.*; public class SimpleReader extends Applet { public void init() { try { FileInputStream f = new FileInputStream(”Saver.tmp”); ObjectInput s = new ObjectInputStream(f); Button b =(Button)s.readObject(); add(b); } catch (Exception e) { System.out.println(e); } } } While the model used for serialization is very simple, it has some drawbacks. First, it’s not as simple as marking serializable classes with the Serializable interface. It is possible common, in fact for an object that can’t be serialized to implement Serializable (either directly or by inheritance). Ultimately, serialization has to do with the data members of the class, not the methods it contains; after all, Serializable is an empty interface and doesn’t require you to implement any methods. A class is serializable if, and only if, it has no members that aren’t serializable specifically: no non-static, non-transient members. By default, static and transient members are ignored when an object is serialized. Generally speaking, classes that belong to the standard Java distribution are serializable unless serializing an object of that class would be a security risk. The problem is that there are many standard classes that would present security risks if serialized for example, a FileInputStream can’t be serialized, because when it is deserialized at a later time (and possibly on a different machine), you have an object that references some file handle that may no longer be meaningful, or that may point to a different file than it did originally. You should make it a practice to check the class of any data members you add to a serializable class to make sure that those data members can be serialized also. Don’t make any assumptions; just look it up in the documentation. Stating that a class implements Serializable is essentially a promise that the class can be successfully saved and restored using the serialization mechanism. The problem is that any subclass of that class automatically implements Serializable via inheritance, even if it adds some nonserializable members. Java throws a NotSerializableException (from the java.io package) if you try to save or restore a non-serializable object. This probably isn’t what you want to happen. When you are writing Beans (or any class that you may want to serialize), you have to think carefully about what the class contains, and you also have to think about how the class will be used. As we’ll see, you can redesign almost any class so that it is serializable, but this redesign may have implications for the interface between your class and the rest of the world. Ultimately, that’s the trick with object serialization. It’s not as simple as marking a few classes Serializable; it has real implications for how you write code. page 73
Note: If you are looking for good and high quality web space to host and run your java application check Vision java hosting services