Explain serialVersionUID in Java

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:

ANY-ACCESS-MODIFIER static final long serialVersionUID = 42L;

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.

Example demonstrating the use of serialVersionUID :

package com.G2.Serialization;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.Serializable;

class Demo1 implements Serializable {

	public Demo1() {
	}
}

public class ExternizationDemo {
	public static void main(String[] args) throws IOException, ClassNotFoundException {
		Demo1 b1 = new Demo1();

		ObjectOutputStream o = new ObjectOutputStream(new FileOutputStream("Demo1.srz"));
		o.writeObject(b1);
		o.close();

	}
}

Above program will run perfectly. now add one more parameter in above class and lets try below example.

package com.G2.Serialization;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.Serializable;

class Demo1 implements Serializable {
	String NeedOfSerial = "Very imp";
	public Demo1() {
	}
}

public class ExternizationDemo {
	public static void main(String[] args) throws IOException, ClassNotFoundException {
		Demo1 b1 = new Demo1();

		 ObjectInputStream in = new ObjectInputStream(new
		 FileInputStream("Demo1.srz"));
		 b1 = (Demo1) in.readObject();
		 System.out.println(" --- Object recovered --- ");

	}
}

Output will be something like below message :

Exception in thread “main” java.io.InvalidClassException: com.G2.Serialization.Demo1; local class incompatible: stream classdesc serialVersionUID = -9110957255177458835, local class serialVersionUID = 4792304913344431293
at java.io.ObjectStreamClass.initNonProxy(Unknown Source)
at java.io.ObjectInputStream.readNonProxyDesc(Unknown Source)
at java.io.ObjectInputStream.readClassDesc(Unknown Source)
at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.readObject(Unknown Source)
at com.G2.Serialization.ExternizationDemo.main(ExternizationDemo.java:26)

By adding below line in class Demo1, above problem will be resolved.

private static final long serialVersionUID = 998985689L;

value of serialVersionUID can be anything unique to the class.

Posted

in

by

Tags:


Related Posts

Comments

One response to “Explain serialVersionUID in Java”

  1. […] unique serialVersionUID with each serialized undergoing class. To read in detail with example, refer this article. Sharevar dzone_url = "https://jitendrazaa.com/blog/java/java-j2ee-interview-questions-1/"; var […]

Leave a Reply

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

Discover more from Jitendra Zaa

Subscribe now to keep reading and get access to the full archive.

Continue Reading