C

Core Java tutorial for beginners

Clean • Professional

Object Serialization in Java – Using transient and serialVersionUID

2 minute

Object Serialization: Using Transient and serialVersionUID

This topic is part of Java Serialization and explains two important concepts used when serializing objects:

learn code with durgesh images

  1. transient keyword
  2. serialVersionUID (Version UID)

Both help control how objects are stored and restored during serialization.

transient Keyword

The transient keyword is used to skip a field during serialization.

Prevents sensitive or unnecessary data from being saved in a serialized file.

Example

import java.io.Serializable;

class User implements Serializable {
    String username;
    transient String password;   // will NOT be serialized
}

When serialized, password will be stored as null (default value).

Why Do We Use transient?

To prevent serialization of:

  • Sensitive data (passwords, OTP, tokens)
  • Derived values (calculated fields)
  • Temporary or cache-like data
  • Fields not needed after deserialization

What Happens Without transient?

Without transient, every non-static field is serialized, which may cause:

  • Security issues (saving passwords)
  • Performance issues (saving unnecessary fields)
  • Exceptions (if the field type is not serializable)

Key Points About transient

  • Used to exclude fields from serialization
  • Static fields are automatically not serialized
  • transient + static = still NOT serialized
  • Useful for security and performance

serialVersionUID (Object Version Control)

serialVersionUID is a unique identifier for a class that is used during serialization and deserialization.

It ensures that the sender and receiver of a serialized object have the same class structure.

If not, it throws:

InvalidClassException

To avoid this, we define a fixed serialVersionUID.

Example

class Student implements Serializable {
    private static final long serialVersionUID = 1L;

    int id;
    String name;
}

Why Manually Declare serialVersionUID?

If not declared manually:

  • JVM auto-generates it based on class structure
  • Small changes break compatibility
  • Old serialized files become unreadable

Declaring your own ID solves this.


When to Change the serialVersionUID?

Change only when:

  • The class structure changes in a way that breaks compatibility
  • Old serialized objects should no longer work

Don't change for minor edits like comments, formatting, etc.


Putting It All Together

class Employee implements Serializable {
    private static final long serialVersionUID = 100L;

    int id;
    String name;

    transient double salary;   // not serialized
}
  • id, name → serialized
  • salary → skipped

Difference Between transient and serialVersionUID

FeaturetransientserialVersionUID
PurposeSkip field during serializationVersion control of serialized class
Applies toVariablesClass
TypeKeywordConstant long value
Effect on SerializationField is not savedEnsures compatibility while saving/loading objects
Used ForSecurity, performance, exclusionAvoiding class mismatch errors

 

Article 0 of 0