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

transient keywordserialVersionUID (Version UID)Both help control how objects are stored and restored during serialization.
transient KeywordThe 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).
transient?To prevent serialization of:
transient?Without transient, every non-static field is serialized, which may cause:
transientserialVersionUID (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;
}
If not declared manually:
Declaring your own ID solves this.
Change only when:
Don't change for minor edits like comments, formatting, etc.
class Employee implements Serializable {
private static final long serialVersionUID = 100L;
int id;
String name;
transient double salary; // not serialized
}
id, name → serializedsalary → skipped| Feature | transient | serialVersionUID |
|---|---|---|
| Purpose | Skip field during serialization | Version control of serialized class |
| Applies to | Variables | Class |
| Type | Keyword | Constant long value |
| Effect on Serialization | Field is not saved | Ensures compatibility while saving/loading objects |
| Used For | Security, performance, exclusion | Avoiding class mismatch errors |