Scrivio Interview library
ConceptFoundations6 min read · updated 12 November 2026

What is serialVersionUID, what happens if you do not declare it, and when should you change it?

serialVersionUID is a long that acts as the version number of a class's serialized form; it is written into every stream and compared with the local class when reading. If we do not declare one, Java computes it from details of the class such as its name, interfaces, fields and methods, so even harmless changes produce a new value and old data fails with InvalidClassException. We declare private static final long serialVersionUID explicitly, and change it only when we make an incompatible change and want old streams rejected.

Why this question is asked

This is one of the most common serialization questions, and also one of the most practical: the missing serialVersionUID is behind many "local class incompatible" errors after a harmless-looking release, when sessions, caches or queued messages written by the old version can no longer be read. Interviewers ask it to see whether we understand what the number protects, what Java does without it, and which changes are actually compatible. In this answer, we will recap where the number appears, reproduce the failure when it is missing, see how the default is computed, declare it properly, and then decide when to change it, with a table of compatible and incompatible changes.

1A Quick Recap: Where the Number Lives

When ObjectOutputStream writes an object, it writes a class descriptor before the field values: the class name, the serialVersionUID and the list of field names and types. When ObjectInputStream reads the stream, it loads the local class with that name and compares the two serialVersionUID values. If they match, it maps the stream's fields onto the local class's fields by name. If they do not, it refuses to read the object.

2Reproducing the Failure When It Is Missing

Here is a class without an explicit serialVersionUID, written into a cache by version 1 of a service.

Java
// Version 1
public class Customer implements Serializable {
private String name;
private String email;
}
// A Customer is written to a cache or a file.
// Version 2: one harmless-looking method added
public class Customer implements Serializable {
private String name;
private String email;
public String displayName() { return name + " <" + email + ">"; }
}
// Reading the old bytes with version 2:
// java.io.InvalidClassException: Customer; local class incompatible:
// stream classdesc serialVersionUID = 4127365390123456789,
// local class serialVersionUID = -8812345678901234567

The two versions are perfectly compatible in their data: the same two fields. Yet the read fails, because without a declared value Java computed a different number for each version. The numbers in the message are whatever the computation produced; ours will differ.

3How the Default Value Is Computed

When a class does not declare serialVersionUID, the JVM computes one by hashing (SHA) a description of the class: its name and modifiers, the interfaces it implements, its fields with their modifiers and types, its constructors and its non-private methods with their signatures, and whether it has a static initialiser. It takes the first eight bytes of the hash as the value.

That is why almost any change alters it: adding a public method, as above, changes the method list. Worse, the computation can depend on members the compiler generates, such as synthetic accessors for inner classes or methods for lambdas and assertions, so the same source compiled by a different compiler can produce a different value. That is why the Serializable documentation strongly recommends declaring the value explicitly in every serializable class. The JDK's serialver tool prints the computed value of an existing class, which is useful when adding the declaration to a class whose old data must still be readable.

4Declaring It Explicitly

We declare it as a private static final long, and mark it with @Serial so the compiler can check the declaration.

Java
public class Customer implements Serializable {
@Serial
private static final long serialVersionUID = 1L;
private String name;
private String email;
private String phone; // added in version 2: old streams leave it null
}
Customer old = readFromOldBytes(); // works: same serialVersionUID
System.out.println(old.phone()); // null

With a fixed value, the read succeeds, and fields are matched by name. A field added in the new version and missing from the old stream gets its default value, null here, and a field present in the stream but removed from the class is simply ignored. Whether 1L or the old computed value is used does not matter for new classes; for a class that already has stored data, we declare the value serialver reports, so existing streams stay readable.

5When to Change It

The number is a promise that old streams can be read by this class. So we keep it the same for compatible changes, and we change it deliberately when we make an incompatible change and want old data to be rejected with a clear error rather than misread. The serialization specification lists which changes are compatible; the common ones are summarised below.

Is this change compatible with old streams?
ChangeCompatible?What happens to old data
Add a fieldyesnew field gets its default value
Remove a fieldreadable; the spec lists it as incompatiblenew class ignores it; older versions see a default
Add or change methodsyesno effect on the data
Change a field's type (int to long)noInvalidClassException or misread data
Make a field static or transientnoits saved value is lost
Move the class in the hierarchynofields no longer line up
Rename a fieldno in practicelooks like remove plus add: value lost

Notice the last row: renaming a field does not cause an error, it silently loses the value, because matching is by name. For such changes, rather than bumping the number and losing all old data, we can keep the number and handle the old form in a readObject method, reading the old field with in.readFields() and mapping it to the new one. Changing serialVersionUID is the right tool when old data truly cannot be interpreted, for example a cache that can simply be rebuilt, where a clean rejection is better than corrupt objects.

6Records and a Word on Long-Term Storage

Records behave a little differently: a record's serialVersionUID is 0L unless declared, and the requirement for matching values is waived for record classes, because records are always rebuilt through their canonical constructor from named components. Check your version's serialization specification for the details.

More broadly, serialVersionUID only manages compatibility; it does not make Java serialization a good format for data that lives a long time or crosses service boundaries. For those, a format with an explicit schema, such as JSON with a schema or Protocol Buffers, gives clearer evolution rules and avoids the security problems of deserializing untrusted bytes.

7Follow-up questions

Does serialVersionUID need to be unique across classes?

No. It is compared only with the stream's value for the same class name, so many classes can all use 1L. It is a version number for one class, not an identifier.

Why do IDEs warn about a missing serialVersionUID?

Because without it, compatibility depends on the compiler's view of the class rather than on our decision, and a trivial change or a different compiler can make old data unreadable. The warning, from javac's -Xlint:serial or the IDE's inspection, nudges us to make that decision explicitly.

Does a serializable superclass need its own serialVersionUID?

Yes. Each serializable class in the hierarchy has its own class descriptor in the stream, with its own serialVersionUID, and each is checked separately. A missing declaration on an abstract serializable superclass breaks old data just as easily as one on the concrete class.

8Conclusion

In this answer, we saw that serialVersionUID is the version number in each class descriptor, compared with the local class when reading. Without a declaration, Java computes it from the class's name, interfaces, fields, methods and even compiler-generated members, so harmless changes cause InvalidClassException. We declare private static final long serialVersionUID explicitly, using serialver's value for classes with existing data, keep it for compatible changes such as adding fields or methods, and change it only for incompatible changes we want old streams to reject.

What to say in the interviewAbout 90 seconds aloud

Open with

serialVersionUID is the version number of a class's serialized form. It is written into the class descriptor of every stream, and when reading, Java compares it with the local class's value; if they differ, the read fails with InvalidClassException, local class incompatible.

Then, if they want more

If I do not declare it, Java computes one by hashing the class's structure: its name, interfaces, fields, constructors and non-private methods, even whether it has a static initialiser.

So adding a public method, or compiling with a different compiler that generates different synthetic members, changes the value, and data written by the previous release, like sessions or cached objects, suddenly cannot be read even though the fields are identical. That is why I always declare private static final long serialVersionUID, with @Serial, in serializable classes.

For a new class 1L is fine; for an existing class with stored data I use the serialver tool to get the current computed value and declare that.

Then I keep it stable for compatible changes, adding fields, which come back as defaults, or adding methods; removing a field is still readable, though the specification counts it as incompatible because older versions then see a default.

I change it only when I make an incompatible change, like changing a field's type or moving the class in the hierarchy, and I want old streams rejected cleanly instead of misread. Records are a special case where matching is waived.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "What is serialVersionUID, what happens if you do not declare it, and when should you...": https://scrivio-interview-library.netlify.app/java/serialization/041d2022fd/