Skip to main content

The 'transient' keyword in Java

· One min read
  • transient on a field means it won't be serialized
    • those fields will have their default values when deserialized
  • use cases
    • derived or calculated fields
    • non-serializable references
    • fields that do not represent the state of the object
    • sensitive data you don't want to send over the network
  • when paired with the final keyword and newing-up (neither baeldung nor geeksforgeeks suggests a use-case for pairing these keywords)
    • if right hand side is a literal, value will be that literal
    • if a e.g. new String(...), value will be null

References