transient Keyword Usage and Examples
Learn about the Java transient keyword with clear explanations, code examples, and real-world use cases. Understand how to exclude fields from serialization in Java.
This reference explains transient Keyword Usage and Examples through its syntax, examples and boundaries. Use the linked course lessons when the surrounding Java concepts are unfamiliar.
Use this reference
Identify the documented behavior, read a complete example and check its inputs before applying it.
transient Keyword in Java
In Java, the transient keyword is used in the context of serialization. If you're building a class that implements Serializable, you might not want all fields of that class to be saved when the object is written to a file or sent over a network. That’s exactly what transient is for — it tells the Java Virtual Machine (JVM) to skip a particular field during serialization.
What is Serialization?
Serialization is the process of converting an object into a byte stream so that it can be stored in a file or transmitted over a network. Deserialization is the reverse — converting that byte stream back into an object.
Why Use transient ?
There are times when certain fields in a class are either:
- Sensitive (like passwords or PINs)
- Derived from other data and don’t need to be saved
- Not serializable themselves (like certain third-party objects)
In such cases, you can mark those fields as transient, and Java will ignore them during serialization.
Syntax
transient dataType variableName;
Basic Example of transient
import java.io.*;
class User implements Serializable {
String username;
transient String password; // This will not be serialized
User(String username, String password) {
this.username = username;
this.password = password;
}
}
public class TransientExample {
public static void main(String[] args) throws Exception {
User user = new User("john_doe", "mySecret");
// Serialize the user
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("user.ser"));
oos.writeObject(user);
oos.close();
// Deserialize the user
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("user.ser"));
User deserializedUser = (User) ois.readObject();
ois.close();
System.out.println("Username: " + deserializedUser.username);
System.out.println("Password: " + deserializedUser.password);
}
}
Output
Java · example discussed here
import java.io.*;
class User implements Serializable {
String username;
transient String password; // This will not be serialized
User(String username, String password) {
this.username = username;
this.password = password;
}
}
public class TransientExample {
public static void main(String[] args) throws Exception {
User user = new User("john_doe", "mySecret");
// Serialize the user
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("user.ser"));
oos.writeObject(user);
oos.close();
// Deserialize the user
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("user.ser"));
User deserializedUser = (User) ois.readObject();
ois.close();
System.out.println("Username: " + deserializedUser.username);
System.out.println("Password: " + deserializedUser.password);
}
}
Username: john_doe
Password: null
Explanation
Java · example discussed here
import java.io.*;
class User implements Serializable {
String username;
transient String password; // This will not be serialized
User(String username, String password) {
this.username = username;
this.password = password;
}
}
public class TransientExample {
public static void main(String[] args) throws Exception {
User user = new User("john_doe", "mySecret");
// Serialize the user
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("user.ser"));
oos.writeObject(user);
oos.close();
// Deserialize the user
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("user.ser"));
User deserializedUser = (User) ois.readObject();
ois.close();
System.out.println("Username: " + deserializedUser.username);
System.out.println("Password: " + deserializedUser.password);
}
}
Here, the username field is serialized and preserved. However, the password field was marked as transient, so it was ignored during serialization. Upon deserialization, the password becomes null because its value wasn’t stored.
Real-World Use Case
Let’s say you're building a banking application. Your Account object contains a balance and a securityToken. The token is used only during active user sessions and should not be persisted. You can mark the token as transient.
class Account implements Serializable {
String accountNumber;
double balance;
transient String securityToken; // Session-based, not to be persisted
}
Points to Remember
transientonly affects serialization, not the normal object state or method execution.- Static fields are implicitly not serialized — marking them
transienthas no effect. - If you need custom behavior, implement
writeObjectandreadObjectmanually.
Advanced Tip: Custom Serialization Logic
private void writeObject(ObjectOutputStream oos) throws IOException {
oos.defaultWriteObject(); // serialize everything except transient
// Add custom serialization logic here
}
private void readObject(ObjectInputStream ois) throws IOException, ClassNotFoundException {
ois.defaultReadObject(); // restore everything except transient
// Add custom deserialization logic here
}
Summary
The transient keyword in Java gives you control over what gets serialized. It's used to protect sensitive information, exclude irrelevant data, and prevent serialization errors due to non-serializable members.
Using this reference
Check the example’s types, inputs and documented behavior before copying it into an application. Some APIs require external libraries, files or platform services; consult their setup notes.
Related explanations: Java Data Types - Primitive and Non-Primitive, Java Class - Blueprint of Objects, Exceptions in Java Introduction.
Supporting documentation: Official Java learning guide.
Reference checks are optional and do not count toward course progress.