Share on Google+Share on Google+

Java Transient Variable

This this tutorial you will learn about the java transient variable and its use

Java Transient Variables

Before knowing the transient variable you should firs know about the Serialization in java. Serialization means making a object state persistent. By default all the variables of an object becomes persistent. If you does not want to make the state of an object persistent then you can mark that object as persistent. You can also say that the transient variable is a variable whose state does not  Serialized.

An example of transient variable is given below blow, in this example the variable course is made as transient.


public class Student implements Serializable {
	private int rollNo;
	private String name;
	private transient String course;
	private String address;

	public Student() {


	public Student(int rollNo, String name, String course, String address) {
		this.rollNo = rollNo; = name;
		this.course = course;
		this.address = address;

	public void detail() {
		System.out.println("Roll No :- " + this.rollNo);
		System.out.println("Name :- " +;
		System.out.println("Course :- " + this.course);
		System.out.println("Address :- " + this.address);



public class MainClaz {
	public static void main(String[] args) throws IOException, Exception {
		Student student = new Student(1, "Jorge", "M.Tech", "Melbourn");
		ObjectOutputStream in = new ObjectOutputStream(new FileOutputStream(
		ObjectInputStream out = new ObjectInputStream(new FileInputStream(
		Student showStudent = (Student) out.readObject();

When you run this application it will display message as shown below:

Roll No :- 1
Name :- Jorge
Course :- null
Address :- Melbourn

Download Select Source Code


Posted on: July 23, 2011 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Discuss: Java Transient Variable  

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image