InvalidClassException: SerialVersionUID Mismatch

java.io.InvalidClassException: com.example.User; local class incompatible: stream classdesc serialVersionUID = 123, local class serialVersionUID = 456

Quick Answer

The serialized data was written with a different version of the class. Define an explicit serialVersionUID to maintain compatibility across class changes.

Why This Happens

Java serialization uses serialVersionUID to verify that the sender and receiver of a serialized object have compatible class definitions. If you do not declare an explicit serialVersionUID, Java auto-generates one based on class structure, which changes whenever you modify the class.

The Problem

public class User implements Serializable {
    // No explicit serialVersionUID
    private String name;
    // Adding a field changes the auto-generated UID
    private int age;
}

The Fix

public class User implements Serializable {
    private static final long serialVersionUID = 1L; // Explicit UID
    private String name;
    private int age; // Adding fields won't break deserialization
}

Step-by-Step Fix

  1. 1

    Identify the class

    Read the exception message for the class name and the mismatched serialVersionUID values.

  2. 2

    Add explicit serialVersionUID

    Add private static final long serialVersionUID = 1L to the class to prevent auto-generation.

  3. 3

    Handle incompatible data

    If you need to read old serialized data, set the UID to match the old value. For new systems, consider using JSON instead of Java serialization.

Bugsly catches this automatically

Bugsly's AI analyzes this error pattern in real-time, explains what went wrong in plain English, and suggests the exact fix — before your users even report it.

Try Bugsly free