..MindWrite..

Posts Tagged ‘serialization’

What is serialVersionUID in Java?

Posted by guptaradhesh on February 16, 2012

The serialVersionUID is a universal version identifier for a Serializable class. Deserialization uses this number to ensure that a loaded class corresponds exactly to a serialized object. If no match is found, then an InvalidClassException is thrown.

The docs for java.io.Serializable have a great explaination saying:

The serialization runtime associates with each serializable class a version number, called a serialVersionUID, which is used during deserialization to verify that the sender and receiver of a serialized object have loaded classes for that object that are compatible with respect to serialization. If the receiver has loaded a class for the object that has a different serialVersionUID than that of the corresponding sender’s class, then deserialization will result in an InvalidClassException. A serializable class can declare its own serialVersionUID explicitly by declaring a field named “serialVersionUID” that must be static, final, and of type long:

static final long serialVersionUID =543543543545L;  

If a serializable class does not explicitly declare a serialVersionUID, then the serialization runtime will calculate a default serialVersionUID value for that class based on various aspects of the class, as described in the Java(TM) Object Serialization Specification. However, it is strongly recommended that all serializable classes explicitly declare serialVersionUID values, since the default serialVersionUID computation is highly sensitive to class details that may vary depending on compiler implementations, and can thus result in unexpected InvalidClassExceptions during deserialization. Therefore, to guarantee a consistent serialVersionUID value across different java compiler implementations, a serializable class must declare an explicit serialVersionUID value. It is also strongly advised that explicit serialVersionUID declarations use the private modifier where possible, since such declarations apply only to the immediately declaring class–serialVersionUID fields are not useful as inherited members.

Posted in java | Tagged: , , | Leave a Comment »

Java Serialization Api

Posted by guptaradhesh on November 7, 2010

Although I felt I know decently about java serialization, while using it in one my of projects I thought of reading about it more. I found this simple definition and so would like to keep it in store (good things should always be spread):

“We all know the Java platform allows us to create reusable objects in memory. However, all of those objects exist only as long as the Java virtual machine remains running. It would be nice if the objects we create could exist beyond the lifetime of the virtual machine, wouldn’t it? Well, with object serialization, you can flatten your objects and reuse them in powerful ways.

Object serialization is the process of saving an object’s state to a sequence of bytes, as well as the process of rebuilding those bytes into a live object at some future time. The Java Serialization API provides a standard mechanism for developers to handle object serialization. The API is small and easy to use, provided the classes and methods are understood.”

An object is marked serializable by implementing the java.io.Serializable interface, which signifies to the underlying API that the object can be flattened into bytes and subsequently inflated in the future.

You can write an object of any kind to a stream. This could be to a disk file or to a Server via a Socket, etc.

You will use the ObjectOutputStream and ObjectInputStream classes.  The constructor for the first takes an OutputStream argument; the constructor for the second takes an InputStream argument.  You can open files to get these, or in the case of client-server Socket communications, you can use methods of the Socket class to obtain these I/O streams:

ObjectOutputStream objOut = new ObjectOutputStream(socket.getOutputStream());
ObjectInputstream objIn  = new ObjectInputStream(socket.getInputStream());

More details: http://java.sun.com/developer/technicalArticles/Programming/serialization/

Posted in java, tech | Tagged: , | 1 Comment »