VOOZH about

URL: https://www.geeksforgeeks.org/java/transient-keyword-java/

⇱ transient keyword in Java - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

transient keyword in Java

Last Updated : 23 Jan, 2026

In Java, the transient keyword is used to exclude specific fields of an object from being serialized. It ensures that sensitive or unnecessary data is not saved when the object is converted into a byte stream.

  • Prevents sensitive information (like passwords) from being serialized.
  • Ensures certain fields are not persisted when an object is written to a file or sent over a network.
  • The default Serialization process ignores fields declared as transient.
  • Transient fields are initialized with default values during deserialization.

Explanation:

  • password and age are marked transient, so they are not serialized.
  • username, email, and dob are serialized as usual.

transient and static

  • Static variables belong to the class, not the object.
  • They are not part of the serialized state.
  • Using transient with static has no effect.
  • No compilation error occurs.

transient and final

  • Final variables are serialized using their constant value.
  • Declaring a final variable as transient has no impact.
  • No compilation error occurs.

Example: with transient, static, and final


Output
i = 10
j = 20
k = 0
l = 40
m = 50

Explanation:

  • i and j are serialized normally.
  • k is transient, so after deserialization it gets default value 0.
  • l is transient static, so its value remains 40, unaffected by serialization.
  • m is transient final, so it retains its constant value 50.
Comment
Article Tags:
Article Tags: