CodesJava

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.

Example:

TransientExample.java

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 codesjava
 */
class Student implements Serializable{
	//Serial Version UID.
	private static final long serialVersionUID = 1L;
	String name;
	transient String className;
	String rollNo;
 
	//Constructor.
	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){
	 try{
           //Creating FileOutputStream object.
	   FileOutputStream fos = 
	    new FileOutputStream("F:\\New folder\\studentTransient.ser");
 
	    //Creating ObjectOutputStream object.
	    ObjectOutputStream oos = new ObjectOutputStream(fos);
 
	    //write object.
	    oos.writeObject(stu);
 
	    //close streams.
	    oos.close();
	    fos.close();
 
	    System.out.println("Serialized data is saved in " +
	       		"F:\\New folder\\studentTransient.ser");
 
        }catch(IOException e){
	          System.out.println(e);
	      }
	}
 
	//Deserialize a serialize object.
	public void objectDeSerialization(){
	  try{
	    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.
	    ois.close();
	    fis.close();
 
	    System.out.println("Name = " + stu.name);
	    System.out.println("Class Name = " + stu.className);
	    System.out.println("RollNo = " + stu.rollNo);
	   }catch(Exception e){
	          System.out.println(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.
		obj.objectSerialization(stu);
		obj.objectDeSerialization();		
	}
}

Output:

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.
JVM, JRE and JDK.
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
Sign Up/ Sign In
Ask a Question


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 hr@codesjava.com.

Development

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

Copyright © 2018 CodesJava DMCA.com Protection Status SiteMap Reference: Java Wiki