What is the purpose of the transient keyword in Java?
The transient
keyword in Java is used to exclude a field from serialization. When an object is serialized, transient fields are not included in the serialized representation.
Purpose
-
Prevent sensitive data (e.g., passwords) from being serialized.
-
Skip unnecessary or derived fields to save space.
In short: Mark fields as transient if they don’t need to be saved during serialization.