Object Serialization

Not all objects that you pass between virtual machines are remote. In fact, you need to be able to pass the primitive Java datatypes, as well as many basic Java objects, such as a String or a HashMap, that are not remote. When a nonremote object is passed across virtual machine boundaries, it gets passed by value using object serialization instead of the traditional Java RMI way of passing objects, by reference. Object serialization is a feature of the Java 1.1 release that allows objects to be turned into a data stream that you can use the way you use other Java streams—send it to a file, over a network, or to standard output. What is important about this method of passing objects across virtual machines is that changes you make to the object on one virtual machine are not reflected in the other virtual machine.

Most of the core Java classes are serializable. If you wish to build classes that are not remote but need to be passed across virtual machines, you need to make those classes serializable. A serializable class minimally needs to implement java.io.Serializable . For almost any kind of nonsensitive data you might want to serialize, just implementing Serializable is enough. You do not even have to write a method; Object already handles the serialization for you. It will, however, assume that you don’t want the object to be serializable unless you implement Serializable. Example 6.2 provides a simple example of how object serialization works. When you ...

Get Database Programming with JDBC & Java, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.