Learn Core Java on Ram N Java!
Subscribe today for concise, clear, and practical Java tutorials!
SUBSCRIBE TO RAM N JAVA1. The Transient Keyword and Default Values
When you serialize an object, you might not want every single field saved. If you declare a field as transient, Java will skip it during serialization.
Upon deserialization, transient fields automatically receive their language defaults:
- Object references (like
String) are set tonull. - Numeric types (like
int) are initialized to0. - Boolean fields are initialized to
false.
2. Why Static Variables Are Not Serialized
A common question in Java interviews is whether static fields are serialized. The short answer is no. Serialization preserves the state of a specific object instance. Because static variables belong to the class itself rather than any individual instance, they are not saved into the byte stream.
3. Object Graph Traversal & NotSerializableException
During serialization, the runtime navigates the entire object graph. If an encountered object does not implement java.io.Serializable, the JVM throws a NotSerializableException indicating the offending class.
The Fix: If you cannot modify that class to make it serializable, declare the reference holding that object as transient.
Watch More Related Java Tutorials
Deepen your understanding of Java IO and serialization with these tutorials:
Click here to watch in Youtube :
https://www.youtube.com/watch?v=MTRg5ZTbm9c&list=UUhwKlOVR041tngjerWxVccw
Click the below Image to Enlarge
![]() |
| Java Tutorial : Java IO (Java Serialization and Deserialization notes) |

No comments:
Post a Comment