What is the purpose of the serialVersionUID in Java serialization?

serialVersionUID is a unique version identifier used in Java serialization to verify that the sender and receiver of a serialized object have compatible versions of the class. It helps maintain consistency during the deserialization process and prevents objects from being restored using incompatible class definitions.

Key Points

Example

Suppose an object is serialized and stored in a file. Later, the class structure changes and the application attempts to deserialize the old object. Java compares the serialVersionUID values to determine whether the object is compatible with the current class definition.

import java.io.Serializable;

public class Employee implements Serializable {

    private static final long serialVersionUID = 1L;

    private int id;
    private String name;
}

If the class is modified in the future but the serialVersionUID remains compatible, previously serialized objects can still be deserialized successfully.

What Happens Without serialVersionUID?

Example Error

java.io.InvalidClassException

Employee; local class incompatible:

stream classdesc serialVersionUID = 1,

local class serialVersionUID = 2

Benefits of Defining serialVersionUID

Real-World Example

Consider a distributed banking application where account objects are transferred between systems. serialVersionUID ensures that both systems use compatible versions of the Account class before deserializing the object.

Interview Tip

A concise interview answer is

serialVersionUID is a unique version identifier for a Serializable class. During deserialization, Java compares this value with the one stored in the serialized object. If they do not match, an InvalidClassException is thrown, preventing incompatible class versions from being used.

Last updated: 16 July 2026

Previous question: What is serialization in Java?

Next question: What happens if the serialVersionUID of a class changes during deserialization?

More Core Java - Level II interview questions

All Core Java - Level II questions