The transient keyword in Java is used to indicate that a field should not be part of the serialization (which means saved, like to a file) process.

From the Java Language Specification, Java SE 7 Edition, Section 8.3.1.3. transient Fields:

Variables may be marked transient to indicate that they are not part of the persistent state of an object.

For example, you may have fields that are derived from other fields, and should only be done so programmatically, rather than having the state be persisted via serialization.

Here's a GalleryImage class which contains an image and a thumbnail derived from the image:

class GalleryImage implements Serializable
{
    private Image image;
    private transient Image thumbnailImage;

    private void generateThumbnail()
    {
        // Generate thumbnail.
    }

    private void readObject(ObjectInputStream inputStream)
            throws IOException, ClassNotFoundException
    {
        inputStream.defaultReadObject();
        generateThumbnail();
    }    
}

In this example, the thumbnailImage is a thumbnail image that is generated by invoking the generateThumbnail method.

The thumbnailImage field is marked as transient, so only the original image is serialized rather than persisting both the original image and the thumbnail image. This means that less storage would be needed to save the serialized object. (Of course, this may or may not be desirable depending on the requirements of the system -- this is just an example.)

At the time of deserialization, the readObject method is called to perform any operations necessary to restore the state of the object back to the state at which the serialization occurred. Here, the thumbnail needs to be generated, so the readObject method is overridden so that the thumbnail will be generated by calling the generateThumbnail method.

For additional information, the article Discover the secrets of the Java Serialization API (which was originally available on the Sun Developer Network) has a section which discusses the use of and presents a scenario where the transient keyword is used to prevent serialization of certain fields.

Answer from coobird on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › java › transient-keyword-java
transient keyword in Java - GeeksforGeeks
January 23, 2026 - In Java, the transient keyword is used to exclude specific fields of an object from being serialized.
🌐
Baeldung
baeldung.com › home › java › core java › the transient keyword in java
The transient Keyword in Java | Baeldung
May 11, 2024 - The transient keyword is useful in a few scenarios: ... public class Book implements Serializable { private static final long serialVersionUID = -2936687026040726549L; private String bookName; private transient String description; private transient int copies; // getters and setters } Here, we have marked description and copies as transient fields. After creating the class, we’ll create an object of this class: Book book = new Book(); book.setBookName("Java Reference"); book.setDescription("will not be saved"); book.setCopies(25);
🌐
DataCamp
datacamp.com › doc › java › transient
transient Keyword in Java: Usage & Examples
The transient keyword is used in scenarios where certain parts of an object's state should not be saved or transferred, such as sensitive information, temporary data, or data that can be easily recomputed. class ClassName implements Serializable { private transient dataType variableName; } ...
🌐
W3Schools
w3schools.com › java › ref_keyword_transient.asp
Java transient Keyword
Java Examples Java Videos Java ... Plan Java Interview Q&A · ❮ Java Keywords · The transient keyword prevents an attribute from being serialized: import java.io.*; public class Main { public static void main(String[] args) ...
Top answer
1 of 15
1847

The transient keyword in Java is used to indicate that a field should not be part of the serialization (which means saved, like to a file) process.

From the Java Language Specification, Java SE 7 Edition, Section 8.3.1.3. transient Fields:

Variables may be marked transient to indicate that they are not part of the persistent state of an object.

For example, you may have fields that are derived from other fields, and should only be done so programmatically, rather than having the state be persisted via serialization.

Here's a GalleryImage class which contains an image and a thumbnail derived from the image:

class GalleryImage implements Serializable
{
    private Image image;
    private transient Image thumbnailImage;

    private void generateThumbnail()
    {
        // Generate thumbnail.
    }

    private void readObject(ObjectInputStream inputStream)
            throws IOException, ClassNotFoundException
    {
        inputStream.defaultReadObject();
        generateThumbnail();
    }    
}

In this example, the thumbnailImage is a thumbnail image that is generated by invoking the generateThumbnail method.

The thumbnailImage field is marked as transient, so only the original image is serialized rather than persisting both the original image and the thumbnail image. This means that less storage would be needed to save the serialized object. (Of course, this may or may not be desirable depending on the requirements of the system -- this is just an example.)

At the time of deserialization, the readObject method is called to perform any operations necessary to restore the state of the object back to the state at which the serialization occurred. Here, the thumbnail needs to be generated, so the readObject method is overridden so that the thumbnail will be generated by calling the generateThumbnail method.

For additional information, the article Discover the secrets of the Java Serialization API (which was originally available on the Sun Developer Network) has a section which discusses the use of and presents a scenario where the transient keyword is used to prevent serialization of certain fields.

2 of 15
502

Before understanding the transient keyword, one has to understand the concept of serialization. If the reader knows about serialization, please skip the first point.

What is serialization?

Serialization is the process of making the object's state persistent. That means the state of the object is converted into a stream of bytes to be used for persisting (e.g. storing bytes in a file) or transferring (e.g. sending bytes across a network). In the same way, we can use the deserialization to bring back the object's state from bytes. This is one of the important concepts in Java programming because serialization is mostly used in networking programming. The objects that need to be transmitted through the network have to be converted into bytes. For that purpose, every class or interface must implement the Serializable interface. It is a marker interface without any methods.

Now what is the transient keyword and its purpose?

By default, all of object's variables get converted into a persistent state. In some cases, you may want to avoid persisting some variables because you don't have the need to persist those variables. So you can declare those variables as transient. If the variable is declared as transient, then it will not be persisted. That is the main purpose of the transient keyword.

I want to explain the above two points with the following example (borrowed from this article):

package javabeat.samples;
 
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
 
class NameStore implements Serializable{
    private String firstName;
    private transient String middleName;
    private String lastName;

    public NameStore (String fName, String mName, String lName){
        this.firstName = fName;
        this.middleName = mName;
        this.lastName = lName;
    }

    public String toString(){
        StringBuffer sb = new StringBuffer(40);
        sb.append("First Name : ");
        sb.append(this.firstName);
        sb.append("Middle Name : ");
        sb.append(this.middleName);
        sb.append("Last Name : ");
        sb.append(this.lastName);
        return sb.toString();
    }
}

public class TransientExample{
    public static void main(String args[]) throws Exception {
        NameStore nameStore = new NameStore("Steve", "Middle","Jobs");
        ObjectOutputStream o = new ObjectOutputStream(new FileOutputStream("nameStore"));
        // writing to object
        o.writeObject(nameStore);
        o.close();
 
        // reading from object
        ObjectInputStream in = new ObjectInputStream(new FileInputStream("nameStore"));
        NameStore nameStore1 = (NameStore)in.readObject();
        System.out.println(nameStore1);
    }
}

And the output will be the following:

First Name : Steve
Middle Name : null
Last Name : Jobs

Middle Name is declared as transient, so it will not be stored in the persistent storage.

🌐
Medium
medium.com › google-developer-experts › diving-deeper-into-the-java-transient-modifier-3b16eff68f42
Diving deeper into the Java transient modifier | by Enrique López-Mañas | Google Developer Experts | Medium
November 30, 2016 - If you serialise and then deserialize now an object of type User, the value password will be null afterwards, since it has been marked as transient. See the annotations @Data, @NoArgsConstructor and @AllArgsConstructor? They are provided by Lombok, a Java library that makes things easier. Although in 2016 Lombok is not as useful as it was before (now languages like Kotlin generate setters and getters automatically, and you can do it with two clicks in any major Development Environment and Eclipse) I still like to use it in certain domains to keep a clean collection of Domain Models.
🌐
Medium
medium.com › @tuananhbk1996 › what-is-the-purpose-of-the-transient-keyword-in-java-how-to-effectively-use-it-57f5dfa17f71
What is the Purpose of the “Transient” Keyword in Java? How to Effectively Use It? | by Anh Trần Tuấn | Medium
August 20, 2024 - The transient keyword in Java is a modifier applied to class fields to indicate that they should not be serialized. When an object is serialized, all its fields are converted into a byte stream.
Find elsewhere
🌐
Unstop
unstop.com › home › blog › transient keyword in java | syntax, uses, & more (+examples)
Transient Keyword In Java | Syntax, Uses, & More (+Examples)
November 19, 2024 - The transient keyword in Java programming is a modifier used to declare that a variable should not be serialized. When an object is serialized, the fields marked as transient are ignored, and their values are not saved to the file or transmitted ...
🌐
Java Guides
javaguides.net › 2018 › 06 › java-transient-keyword.html
Java Transient Keyword
June 15, 2024 - The transient keyword in Java is used for controlling the serialization process. By marking fields as transient, you can exclude them from being serialized, which is useful for sensitive data, temporary state, or derived fields.
🌐
Scaler
scaler.com › home › topics › transient variable in java
Transient Variable in Java - Scaler Topics
May 5, 2024 - For example, for an object transient variable, this behaviour can be customized using a Custom Serialized form or through a custom Externalizable interface. With a transient variable, you can prevent any object from being serialized. The transient keyword can be used to make any variable transient.
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › beans › Transient.html
Transient (Java Platform SE 8 )
April 21, 2026 - Java™ Platform Standard Ed. 8 ... Indicates that an attribute called "transient" should be declared with the given value when the Introspector constructs a PropertyDescriptor or EventSetDescriptor classes associated with the annotated code element.
🌐
EDUCBA
educba.com › home › software development › software development tutorials › java tutorial › java transient
Java Transient | Functions of Transient Keyword in Java with Examples
April 10, 2023 - Java transient is a modifier keyword that is used to mark a variable whose value is not to be serialized during Serialization. If we do not want to save the value of a specific variable during the serialisation, then we can mark a variable as ...
Address   Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai
🌐
Quora
quora.com › What-is-the-purpose-of-Javas-transient-keyword-when-applied-to-instance-variables
What is the purpose of Java's 'transient' keyword when applied to instance variables? - Quora
Answer: Transient in Java is used to mark the member variable not to be serialized when it is persisted to streams of bytes. This keyword plays an important role to meet security constraints in Java. It ignores the original value of a variable and saves the default value of that variable data type.
🌐
JustAnswer
justanswer.com › computer-programming › mtn0c-transient-keyword-discuss-purpose-transient.html
Transient Keyword: Discuss the purpose of the transient keyword when applied to a variable in a Java class. Explain its
The transient keyword in Java marks variables to be excluded from serialization, preventing their values from being saved or restored. This is crucial for sensitive or non-serializable fields.
🌐
Scribd
scribd.com › document › 561660422 › Java-transient-Keyword
Understanding Java's Transient Keyword | PDF | Class (Computer Programming) | Java (Programming Language)
The transient keyword in Java is used to exclude object fields from serialization. During serialization, fields marked as transient will not be written to the output stream. This is useful when an object field derives from other fields or does ...
🌐
Medium
medium.com › @makhaer › java-transient-serialization-deserialization-for-beginners-2fd4d629ba65
Java transient, serialization, deserialization for beginners. | by Mohammad Khaer MAK | Medium
March 6, 2024 - Derived Data: Fields whose values are constant or can be recalculated based on other data may be marked as transient to prevent unnecessary serialization overhead. Lets start with a real life example. Say you have a gift to send to your friend who is living 100 miles away. In order to send this gift to your friend you will pack it in a box, seal it and send to your friend through some delivery service. In the context of programming and Java serialization is the process of putting objects (say the gift here) into a format (like a box) that can be easily saved to a file, sent over the internet, or stored in some other way.
🌐
W3Schools
w3schools.com › java › java_ref_keywords.asp
Java Keywords
Java Reference Java Keywords · assert abstract boolean break byte case catch char class continue default do double else enum exports extends final finally float for if implements import instanceof int interface long module native new package private protected public return requires short static super switch synchronized this throw throws transient try var void volatile while Java String Methods ·
🌐
CodeGym
codegym.cc › java blog › random › transient keyword in java
transient keyword in Java
March 4, 2025 - In Java, transient is how you mark that page tells the system, “Yo, don’t pack this bit when you’re saving or sending me.” It’s all tied to serialization, which is just a fancy way of saying “squishing an object into bytes” to ...