Serialization
Serialization is a process of writing an object. It is a process of saving an object’s state to a sequence of bytes, as well as the process¬†of rebuilding those bytes back into a live object at some future times.

An object is marked serializable by implementing the java.io.Serializable interface, which is only a marker interface — it provides some functionality serialization mechanism to verify that the class can be persisted, typically to a file.

Transient variables cannot be serialized. The fields marked transient in a serializable object will not be transmitted in the byte stream.
An example would be a file handle, a database connection, a system thread etc. Such objects are only meaningful locally. So they should be marked as transient in a serializable class.

Serialization can adversely affect performance since it:

1. Depends on reflection

2. Has an incredibly verbose format.

3. Is very easy to send surplus data.

When to use Serialization?
Do not use serialization if you don’t have to.
A common use of serialization is to use it to send an object over the network or if the state of an object needs to be persisted to a flat file or a database. Deep cloning or the copy can be achieved through the serialization. This may be fast to code but will have performance implications.
The objects stored in an HTTP session should be serializable to support in-memory replication of sessions to achieve scalability.
Objects are passed in RMI(Remote Method Invocation) across the network using serialization.

What is serial version id?
Say you can create a class “Car”, instantiate it and write it out to an object stream. The flattened car object sits in the file system for some time. Meanwhile, if the “Car” class is modified by adding a new field. Later on, when you try to read i.e deserialize the flattened “Car”¬†object, you get the java.io.InvalidClassException – because all serializable classes are automatically given a unique identifier.

This exception is thrown when the identifier of the class is not equal to the identifier of the flattened object. If you really think about it, the exception is thrown because of the addition of the new field. You can avoid this exception being thrown by controlling the versioning yourself by declaring an explicit serialVersionUID.

There is a small performance benefit in explicitly declaring your serialVersionUID(because does not have to be calculated).
So it is best practice to add your own serialVersionUID to your Serializable classes as soon as you create them as shown below:

 

public Class Car{
static final long serialVersionUID =1L; //assign a long value

}

 

Note:
Alternatively you can use the serialver tool comes with Sun’s JDK. This tool takes a full class name on the command line and returns the serialVersionUID for that compiled class.

Amit is a software developer by profession. He is passionate about all the technologies which can be used to build an application like java related technologies and many more and loves to implement it his career or for knowledge purpose and share his experience with you all. To know more see the About me profile.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.