Tutorialspoint examples

Learn java tutorial with examples for beginners online

Transient keyword in java

Transient variable:

A variable declared with transient keyword is a transient variable. It is used in serialization process to provide flexibility for excluding some variables from serialization process. A variable declared with transient keyword will not be serialized.



import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
 * This program is used to show that transient variables 
 * are not saved in serialization process.
 * @author javawithease
class Student implements Serializable{
	//Serial Version UID.
	private static final long serialVersionUID = 1L;
	String name;
	transient String className;
	String rollNo;
	Student(String name, String className, String rollNo){
		this.name = name;
		this.className = className;
		this.rollNo = rollNo;
class Test{
	//Write serialized object into objectoutputstream.
	public void objectSerialization(Student stu){
           //Creating FileOutputStream object.
	   FileOutputStream fos = 
	    new FileOutputStream("F:\\New folder\\studentTransient.ser");
	    //Creating ObjectOutputStream object.
	    ObjectOutputStream oos = new ObjectOutputStream(fos);
	    //write object.
	    //close streams.
	    System.out.println("Serialized data is saved in " +
	       		"F:\\New folder\\studentTransient.ser");
        }catch(IOException e){
	//Deserialize a serialize object.
	public void objectDeSerialization(){
	    Student stu = null;
	    //Creating FileOutputStream object.
	    FileInputStream fis = 
	     new FileInputStream("F:\\New folder\\studentTransient.ser");
	    //Creating ObjectOutputStream object.
	    ObjectInputStream ois = new ObjectInputStream(fis);
	    //write object.
	    stu = (Student) ois.readObject();
	    //close streams.
	    System.out.println("Name = " + stu.name);
	    System.out.println("Class Name = " + stu.className);
	    System.out.println("RollNo = " + stu.rollNo);
	   }catch(Exception e){
public class TransientExample {
	public static void main(String args[]){	
		//Creating Student object.
		Student stu = 
                  new Student("Parmander", "MCA", "MCA/07/27");		
		//Creating Test object.
		Test obj = new Test();		
		//Method call.


Serialized data is saved in F:\New folder\studentTransient.ser
Name = Parmander
Class Name = null
RollNo = MCA/07/27

Download this example.

Which types of variables are not serialized during Java Serialization?

Static and transient variables are not serialized during Java Serialization. As static are associated to the class and not to an object hence they are not the part of the state of object so they are not saved during Java Serialization process.
Next Topic: Collection framework in java.
Previous Topic: Serialization in java with example.

Related Topics:

Input output (I/O) in java.
FileInputStream and FileOutputStream in java with example.
Byte Streams in java with example.
DataInputStream and DataOutputStream in java with example.
BufferedInputStream and BufferedOutputStream in java with example.
FileReader and FileWriter in java with example.
How To Check If A File Exists In Java.
Serialization in java with example.
Transient in java with example.


Download tutorialspointexamples Android App
Copyright © 2018 Tutorialspoint examples DMCA.com Protection Status