What is transient keyword in Java

๐Ÿ’ก Concept: Transient Keyword

The transient keyword in Java marks a field to be skipped during serialization.

๐Ÿ“˜ Quick Intro

Fields marked as transient are not included in the serialized representation of an object.

๐Ÿง  Analogy

Transient is like putting aside some personal items before sending a package, so they are not shipped.

๐Ÿ”ง Technical Explanation

  • Transient fields are ignored by Java serialization.
  • Useful for sensitive data or temporary states.
  • Does not affect deserialization of other fields.
  • Transient can be used with any data type.
  • Static fields are never serialized, regardless of transient.

๐ŸŽฏ Use Cases

  • โœ… Skip sensitive data like passwords during serialization.
  • โœ… Avoid serializing temporary or cached data.
  • โœ… Reduce serialized object size.

๐Ÿ’ป Example: Using transient keyword


import java.io.Serializable;

public class User implements Serializable {
    private String username;
    private transient String password; // will not be serialized

    public User(String username, String password) {
        this.username = username;
        this.password = password;
    }
    // getters and setters
}

โ“ Interview Q&A

Q1: What does transient do?
A: Prevents field serialization.

Q2: Can transient fields be restored?
A: No, they are null/default after deserialization.

Q3: Can static fields be transient?
A: Yes, but static fields are not serialized anyway.

Q4: When to use transient?
A: For sensitive or temporary data.

Q5: Does transient affect serialization of other fields?
A: No.

Q6: Is transient a keyword or annotation?
A: It is a keyword.

Q7: Does transient work with custom serialization?
A: Yes, but must be handled manually.

Q8: Can final fields be transient?
A: Yes.

Q9: Is transient specific to Java?
A: Yes, in Java serialization.

Q10: What happens to transient fields on deserialization?
A: They get default values (null, 0, false).

๐Ÿ“ MCQs

Q1. What does transient do?

  • Includes field
  • Prevents field serialization
  • Deletes field
  • Copies field

Q2. Can transient fields be restored?

  • Yes
  • No
  • Sometimes
  • Depends

Q3. Can static fields be transient?

  • No
  • Yes, but not serialized anyway
  • Only static
  • Only transient

Q4. When to use transient?

  • Always
  • Never
  • Sensitive or temporary data
  • Only final fields

Q5. Does transient affect other fields?

  • Yes
  • No
  • Sometimes
  • Depends

Q6. Is transient a keyword or annotation?

  • Keyword
  • Annotation
  • Method
  • Class

Q7. Does transient work with custom serialization?

  • No
  • Yes
  • Sometimes
  • Never

Q8. Can final fields be transient?

  • No
  • Yes
  • Sometimes
  • Never

Q9. Is transient specific to Java?

  • No
  • Yes
  • Sometimes
  • Depends

Q10. What happens to transient fields on deserialization?

  • Original values
  • Default values
  • Null
  • Error

๐Ÿ’ก Bonus Insight

Using transient wisely helps secure sensitive data and optimize serialized objects in Java applications.

๐Ÿ“„ PDF Download

Need a handy summary for your notes? Download this topic as a PDF!

๐Ÿ” Navigation

๐Ÿ’ฌ Feedback
๐Ÿš€ Start Learning
Share:

Tags: