- 1Java Exceptions
- 2Java Keywords
- 3Java abstract Keyword
- 4Java assert Keyword
- 5Java boolean Keyword
- 6Java break Keyword
- 7Java byte Keyword
- 8Java case Keyword
- 9Java catch Keyword
- 10Java char Keyword
- 11Java class Keyword
- 12Java const Keyword
- 13Java continue Keyword
- 14Java default Keyword
- 15Java do Keyword
- 16Java double Keyword
- 17Java else Keyword
- 18Java enum Keyword
- 19Java extends Keyword
- 20Java final Keyword
- 21Java finally Keyword
- 22Java float Keyword
- 23Java for Keyword
- 24Java goto Keyword
- 25Java if Keyword
- 26Java implements Keyword
- 27Java import Keyword
- 28Java instanceof Keyword
- 29Java int Keyword
- 30Java interface Keyword
- 31Java long Keyword
- 32Java native Keyword
- 33Java new Keyword
- 34Java null Keyword
- 35Java package Keyword
- 36Java private Keyword
- 37Java protected Keyword
- 38Java public Keyword
- 39Java return Keyword
- 40Java short Keyword
- 41Java static Keyword
- 42Java strictfp Keyword
- 43Java super Keyword
- 44Java switch Keyword
- 45Java synchronized Keyword
- 46Java this Keyword
- 47Java transient Keyword
- 48Java try Keyword
- 49Java void Keyword
- 50Java volatile Keyword
- 51Java while Keyword
- 52Java String Methods - Syntax and Description
- 53Java String
charAt()
method - 54Java String
codePointAt()
method - 55Java String
codePointBefore()
method - 56Java String
codePointCount()
method - 57Java String
compareTo()
method - 58Java String
compareToIgnoreCase()
method - 59Java String
concat()
method - 60Java String
contains()
method - 61Java String
contentEquals()
method - 62Java String
copyValueOf()
method - 63Java String
endsWith()
method - 64Java String
equals()
method - 65Java String
equalsIgnoreCase()
method - 66Java String
format()
method - 67Java String
getBytes()
method - 68Java String
getChars()
method - 69Java String
hashCode()
method - 70Java String
indexOf()
method - 71Java String
intern()
method - 72Java String
isEmpty()
method - 73Java String
join()
method - 74Java String
lastIndexOf()
method - 75Java String
length()
method - 76Java String
matches()
method - 77Java String
offsetByCodePoints()
method - 78Java String
regionMatches()
method - 79Java String
replace()
method - 80Java String
replaceAll()
method - 81Java String
replaceFirst()
method - 82Java String
split()
method - 83Java String
startsWith()
method - 84Java String
subSequence()
method - 85Java String
substring()
method - 86Java String
toCharArray()
method - 87Java String
toLowerCase()
method - 88Java String
toString()
method - 89Java String
toUpperCase()
method - 90Java String
trim()
method - 91Java String
valueOf()
method - 92Java ArrayList Methods - Complete Reference with Syntax and Description
- 93Java LinkedList Methods - Complete Reference with Syntax and Description
- 94Java HashMap Methods - Syntax and Descriptions
Java transient Keyword
Usage and Examples
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
Username: john_doe
Password: null
Explanation
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
transient
only affects serialization, not the normal object state or method execution.- Static fields are implicitly not serialized — marking them
transient
has no effect. - If you need custom behavior, implement
writeObject
andreadObject
manually.
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.