Java Transient Variable

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

Ads

Tutorials   
How to read properties file in Java? Generate random numbers in Java Cloneable Interface in Java What is the flaw with the Stack class? List In Java Java Stack Example HashSet In Java Enumeration in java How to generate random number in java Iterator in java Queue in java Transient Java Keyword clone method in Java Java Queue example Logger in Java Java current date - How to get current date in Java Collection : LinkedList Example Collection : ArrayList Example Random alpha numeric string of fixed size Java List Iterator Example The Hashtable Class The BitSet class Java Vector Java Enumeration Data Structures in Java Java Transient Variable Java Date Format Example Java Properties File Example Map.Entry Java Interface Java Property File java preferences,Change the value of Preferences by user Retrieving a Preference Node Java hasNext Write Keys and Values to the Properties file in Java Get All Keys and Values of the Properties files in Java Properties file in Java Properties file in Java Determining If a Preference Node Contains a Specific Value Change the value of Preferences by user Clone method example in Java Implementing a Simple Event Notifier Determining When a Preference Node Is Added or Removed Listening for Changes to Preference Values in a Preference Node Importing Preferences Finding a Preference in a Preference Tree Retrieving the Parent and Child Nodes of a Preference Node Determining If a Preference Node Exists Removing a Preference Node Creating a Preference Node Getting the Maximum Size of a Preference Key and Value
Ads

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.

Student.java

import java.io.Serializable;

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;
		this.name = name;
		this.course = course;
		this.address = address;
	}

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

}

MainClaz.java

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

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(
				"LogFile.text"));
		in.writeObject(student);
		in.close();
		Thread.sleep(500);
		ObjectInputStream out = new ObjectInputStream(new FileInputStream(
				"LogFile.text"));
		Student showStudent = (Student) out.readObject();
		showStudent.detail();
	}
}


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

Advertisements

Ads
Share on Google+Share on Google+

Java Transient Variable

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

Advertisements

 

Discuss: Java Transient Variable  

Post your Comment


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

Ads

 

Ads