0% found this document useful (0 votes)
69 views

Java Transient Keyword

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 not represent the true state of the object. For example, a password field could be marked transient so the original password is not stored in the serialized file. The example demonstrates a Student class with id, name, and age fields, but age is marked transient so it is not serialized and defaults to 0 on deserialization.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
69 views

Java Transient Keyword

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 not represent the true state of the object. For example, a password field could be marked transient so the original password is not stored in the serialized file. The example demonstrates a Student class with id, name, and age fields, but age is marked transient so it is not serialized and defaults to 0 on deserialization.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Java transient Keyword

In Java, Serialization is used to convert an object into a stream of the byte. The byte stream consists of the
data of the instance as well as the type of data stored in that instance. Deserialization performs exactly
opposite operation. It converts the byte sequence into original object data. During the serialization, when
we do not want an object to be serialized we can use a transient keyword.

Why to use the transient keyword?

The transient keyword can be used with the data members of a class in order to avoid their serialization.
For example, if a program accepts a user's login details and password. But we don't want to store the
original password in the file. Here, we can use transient keyword and when JVM reads the transient
keyword it ignores the original value of the object and instead stores the default value of the object.

Syntax:

1. private transient <member variable>;  

Or

1. transient private <member variable>;  

When to use the transient keyword?


1. The transient modifier can be used where there are data members derived from the other data
members within the same instance of the class.
2. This transient keyword can be used with the data members which do not depict the state of the
object.
3. The data members of a non-serialized object or class can use a transient modifier.

Example of Java Transient Keyword


Let's take an example, we have declared a class as Student, it has three data members id, name and age.
If you serialize the object, all the values will be serialized but we don't want to serialize one value,
e.g. age then we can declare the age data member as transient.

In this example, we have created two classes Student and PersistExample. The age data member of


the Student class is declared as transient, its value will not be serialized.

If you deserialize the object, you will get the default value for transient variable.

Let's create a class with transient variable.

PersistExample.java
1. import java.io.*;      
2. public class Student implements Serializable{    
3.  int id;    
4.  String name;    
5.  transient int age;//Now it will not be serialized    
6.  public Student(int id, String name,int age) {    
7.   this.id = id;    
8.   this.name = name;    
9.   this.age=age;    
10.  }    
11. }    
12. class PersistExample{    
13.  public static void main(String args[])throws Exception{    
14.   Student s1 =new Student(211,"ravi",22);//creating object    
15.   //writing object into file    
16.   FileOutputStream f=new FileOutputStream("f.txt");    
17.   ObjectOutputStream out=new ObjectOutputStream(f);    
18.   out.writeObject(s1);    
19.   out.flush();    
20.   out.close();    
21.   f.close();    
22.   System.out.println("success");    
23.  }    
24. }    

Output:

success

Now write the code for deserialization.

DePersist.java

1. import java.io.*;    
2. class DePersist{    
3.  public static void main(String args[])throws Exception{    
4.   ObjectInputStream in=new ObjectInputStream(new FileInputStream("f.txt"));    
5.   Student s=(Student)in.readObject();    
6.   System.out.println(s.id+" "+s.name+" "+s.age);    
7.   in.close();    
8.  }    
9. }    
Output:

211 ravi 0

As you can see, printing age of the student returns 0 because value of age was not serialized.

In this article, we have discussed use of transient keyword in Java, where to use transient keyword

You might also like