If the serialVersionUID of a class changes after an object has been serialized, Java treats the new class version as incompatible with the serialized object. During deserialization, the JVM compares the serialVersionUID stored in the object stream with the serialVersionUID of the current class. If they do not match, deserialization fails and an InvalidClassException is thrown.
private static final long serialVersionUID = 1L;
private static final long serialVersionUID = 2L;
When Java attempts to deserialize the previously stored object, it detects the version mismatch and rejects the operation.
import java.io.Serializable;
public class Employee implements Serializable {
private static final long serialVersionUID = 1L;
private int id;
private String name;
}public class Employee implements Serializable {
private static final long serialVersionUID = 2L;
private int id;
private String name;
}Employee; local class incompatible:
stream classdesc serialVersionUID = 1,
local class serialVersionUID = 2
Why Does Java Do This?
Always declare serialVersionUID explicitly in Serializable classes to maintain control over version compatibility and avoid unexpected deserialization failures.
A concise interview answer is
If the serialVersionUID changes between serialization and deserialization, Java considers the class versions incompatible and throws an InvalidClassException. This mechanism protects the application from loading objects whose structure may no longer match the current class definition.
Last updated: 16 July 2026
Previous question: What is the purpose of the serialVersionUID in Java serialization?
Next question: How can you prevent certain fields from being serialized in Java?