Easy learning with example program codes

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.


 * This program is used to show that transient variables 
 * are not saved in serialization process.
 * @author codesjava
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){ = 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 = " +;
	    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.


Core Java Tutorial

Programming language overview.
Overview of Java.
Java features
JVM architecture details.
Java Coding Guidelines.
Some important definitions.
Variable and data types.
Hello world java program.
Core java examples programs.
Important Java Programs.
How to set permanent path in java?
OOPs Basics.
Object and Class in Java.
OOPs Principles/Concepts.
Abstraction in java.
Encapsulation in java.
Polymorphism in java.
Method overloading in java.
Method overriding in java.
Dynamic method dispatch.
Runtime polymorphism.
Association in java.
Inheritance in java.
Aggregation in java.
Command line arguments in java.
Command line argument program in eclipse.
Read input from command line using Scanner.
Java array programs
Java star pattern programs
Java number pattern programs
final in java.
Abstract class in java.
Interface in java.
Custom marker Interface in java.
Constructor in java.
Package in java.
Access modifier in java.
Static import in java.
Package class in java.
this in java.
Instance initialize block.
Anonymous block.
super in java.
Static in java.
final in java.
Java cloning deep and shallow
Shallow vs Clone copy
String handling in java.
String handling programs.
StringBuffer in java.
StringBuilder in java.
Exception Handling Tutorial.
Multithreading Tutoial.
Java input output stream tutorial.
Collections framework in java tutorial.
Collections framework programs.
Java Random class
Java annotations.
Java design principles.
Java 7 features
Java 8 features
Java networking tutorial
Java Reflection tutorial

Industrial Training

We offers Placement Oriented Training on Java, Spring, JSF, Hibernate, PHP, AngularJS, Angular 4, PLSQL, Oracle BI Publisher etc. We also provides Online training, please mail us at


We also provides the Development services for Website Development , Java Development, PHP Development, Android App Development etc. You can contact us on

Copyright © 2019 CodesJava Protection Status SiteMap Reference: Java Wiki