Java Custom Serialization — Complete Guide With Example
(Updated with transient vs non-transient fields explanation)
Serialization is a core concept in Java and plays a major role in distributed systems, microservices, caching, and message passing.
This blog explains:
-
What serialization is
-
Why serialization is needed
-
What transient & non-transient mean
-
How custom serialization works
-
Real-world use cases
-
Complete code example
1. What Is Serialization in Java?
Serialization is the process of converting an object into a byte stream so it can be:
✔ Stored in a file
✔ Sent over a network
✔ Stored in cache
✔ Persisted temporarily
✔ Shared between JVM instances
Java provides built-in serialization with:
2. Why Serialization Is Needed (Real Use Cases)
Serialization is crucial in many systems:
✔ Distributed Systems
Send objects between services (legacy RMI, microservices, RPC frameworks).
✔ Caching
Store Java objects in Redis, Hazelcast, Ignite.
✔ Message Passing
Kafka, ActiveMQ, RabbitMQ often serialize objects.
✔ File/Session Storage
Store state so it can be restored later.
3. What Are Transient and Non-Transient Fields?
Serialization by default saves every non-transient field of a class.
But sometimes we do NOT want all fields to be serialized.
📍 Transient Field
A transient field is NOT serialized even if the class implements Serializable.
Example:
Java intentionally skips this during serialization.
📍 Non-Transient Field
Normal variables (without transient) are serialized.
Example:
4. Why Java Introduced transient?
(Why do we need it?)
✔ 1. Security
You don’t want to serialize sensitive data:
-
passwords
-
tokens
-
API keys
-
cryptographic secrets
✔ 2. Derived (non-essential) fields
Some fields can be computed again at runtime.
✔ 3. Large fields not needed in serialization
Example: temporary buffers
✔ 4. Prevent unwanted state transfer
Ex: open network connections, thread objects, file streams
These objects cannot be serialized, so marking them transient avoids errors.
✔ 5. Reduce payload
Skipping unnecessary fields reduces storage & network cost.
5. How transient Works Internally?
When serializing:
-
JVM examines all fields
-
If a field is
transient→ it is skipped -
If not transient → JVM serializes it
On deserialization:
-
Transient fields are restored as default values:
-
0for numbers -
falsefor boolean -
nullfor all objects
-
6. Custom Serialization (writeObject & readObject)
Sometimes we need even more control, beyond transient.
Example use case:
Do not serialize Employee object IF address is empty.
For such rules, Java lets you override serialization using:
7. Full Example: Custom Serialization in Java
🟦 Employee class
🟧 Main Class
8. Expected Output
This shows that your custom validation logic executed.
Since address is empty, the object is blocked from serialization.
9. Where Transient + Custom Serialization Are Useful
| Scenario | Use Case | Feature |
|---|---|---|
| Sensitive fields | Password, token, key | transient |
| Skip invalid objects | Missing mandatory data | writeObject() |
| Reduce size | Remove large buffers | transient |
| Derived fields | Recalculate instead of storing | transient |
| Full control over serialization | Business validation | writeObject/readObject |
Often you combine both transient and custom serialization.
No comments:
Post a Comment