transient keyword in java with example

Last updated: September 2026 — rewritten with examples and interview Q&A.

The transient keyword almost always comes up right after serialization in an interview. The interviewer has just asked how serialization works — now they want to know if you understand what gets left out.

What transient does

transient marks a field to be skipped during serialization. When the object is deserialized, transient fields are restored to their default values — 0/false for primitives, null for references. The constructor does not run, so nothing re-initializes them.

It applies to fields only — never to methods or classes. The classic use case: fields that must not travel with the object, like passwords, or fields that can't be serialized, like Thread, Socket, or stream objects (leaving a non-serializable field non-transient gives you NotSerializableException at runtime).

transient in code

public class User implements Serializable {
    private String username;
    private transient String password;  // never serialized

    public User(String username, String password) {
        this.username = username;
        this.password = password;
    }
}
// serialize
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("user.ser"));
out.writeObject(new User("admin", "s3cret"));
out.close();

// deserialize
User restored = (User) new ObjectInputStream(new FileInputStream("user.ser")).readObject();
System.out.println(restored.getUsername());  // "admin"
System.out.println(restored.getPassword());  // null — transient was skipped

Rules interviewers test

  • Fields only. transient on a method or class doesn't compile.
  • Default values on restore. Transient fields come back as 0, false, or null — plan for it.
  • static is also skipped — for a different reason. Static fields belong to the class, not the instance, so they're never part of the object stream. (transient static together is redundant but legal.)
  • Ignored by Externalizable. When you hand-write writeExternal/readExternal, transient means nothing — you choose every byte.
  • Final + transient is a trap. It compiles, but a final transient field is skipped during serialization and can't be reassigned after — so it silently stays at its default. Interviewers love this one.

Interview rapid-fire

  • Can transient be applied to a method? — No. Fields only.
  • What's the value of a transient field after deserialization? — The type's default: 0, false, or null.
  • transient vs static in serialization? — Both are skipped; transient is per-field opt-out, static is skipped because it belongs to the class.
  • How do you keep a password out of the serialized form? — Mark it transient (or use Externalizable and simply don't write it).
  • What if a non-transient field isn't serializable? — NotSerializableException when you call writeObject.

Java serialization: the complete series

Comments

Popular posts from this blog

What are the core classes and interfaces of hibernate 4

JSP Servlet Interview Questions For Freshers Series 1

Java Banking Finance Services and Insurance (BFSI) domain interview questions