• Skip to primary navigation
  • Skip to content
  • Skip to primary sidebar
  • Skip to footer
  • Core Java
  • Design Patterns
  • JSP
  • Servlets
  • Building Tools
  • jQuery
  • Spring
  • Hibernate
  • Mongo DB
  • More
    • HTML
    • SCJP
    • AJAX
    • UML
    • Struts
    • J2EE
    • Testing
    • Angular JS

J2EE Reference

  • Home
  • About Us
    • Java Learning Centers
  • Contact Us

Transient Variable

June 28, 2012 By j2eereference 1 Comment

Here we are going to discuss about Transient variable in java.

Topics Covered :

What is Transient Variable

How to declare Transient Variable

Need of Transient Variable

Some Important Point about transient Variable

Example that demonstrate transient variable

What is Transient Variable

Transient is a java keyword which has some significant meaning that field or variable should not be serialized.

To discuss more about Transient Lets first discuss briefly about Serialization.

Serialization: Object serialization is the process of saving an object’s state to a sequence of bytes and during deserializaiton the saved state can be recovered by JVM.

Thus, transient keyword is applied only to the member variables of a class and it is used to indicate that the member variable should not be serialized when the class containing that transient variable is needed to be serialized.

For more details about serialization click here .

How to declare Transient Variable

transient String authorName= “Shobhna”;

transient : modified variable authorName as transient.

type :type can be any of data type in java.

variableName : variable name is any valid identifier .

In the above statement we have declared the variable autherName as transient. Hense after after deserializing the value we get for this variable will be null.

1
2
3
4
5
package com.j2eereference.coreJava;
 
 public class SerialableClassExample{
  private transient String authorName;
  }

 

The value of the transient variable will be the default value of the data type.

In the above example we are getting as null because of the String data type. If we declare transient  variable as int and tries to retrieve the value , it will return 0.

Need of Transient Variable

Transient variable is used to indicate Java virtual machine that it is not a part of the persistent state of the object.

During serialization process it gives you flexibility so that you can exclude some variables from the serialization process because in Serialization except static and transient variable all property of object gets saved.

Some Important points about transient variable

1) Transient keyword can only be applicable to member variable or field, it is not applicable for methods, class and local variables.

2) Transient variable indicates Java virtual machine that it is not a part of the persistent state of the object.

3) Java compiler does not give compilation error if we declare variable static and transient at same time.

4)we can declare final and transient at same time .But sometimes it will not work as transient variable would not be serialized, so during deserialization  process you have to initialize the field manually, but if it is declared final, the compiler would complaint about it.

5)We can make those variable as transient whose value can be determined from other variables .

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package com.j2eereference.coreJava;
 
 import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
 
 public class TransientExample implements Serializable{
  private int principalamt;
  private int rate;
  private int time;
  private String name;
  transient private int interest;
 TransientExample(int p,int r,int t,String name) {
    this.name = name;
    interest=p*r*t;
  }
 
public static void main(String [] args) {
  TransientExample t = new TransientExample(1000,5,1,"shobhna");
  System.out.println("Before serialization: - " + t.name + " "+ t.interest);
  try {
  FileOutputStream fso = new FileOutputStream("textfile.txt");
  ObjectOutputStream oso = new ObjectOutputStream(fso);
  oso.writeObject(t);
  oso.close();
  } catch (Exception e) {
    e.printStackTrace();
 }
try {
  FileInputStream fsi = new FileInputStream("textfile.ser");
  ObjectInputStream osi = new ObjectInputStream(fsi);
  t = (TransientExample)osi.readObject();
  osi.close();
} catch (Exception e) { e.printStackTrace(); }
  System.out.println("After de-serialization:- " + t.name + " "+ t.interest);
}
 
}

 Output

Before serialization: – shobhna 5000
After de-serialization:- shobhna 0

Here you can notice that after deserialization  we are not getting the interest calculated as we have declared the variable as transient.

Related Posts

  • UncaughtExceptionHandler in java
  • How to generate and resolve OutOfMemoryError?
  • Difference between Spring’s Singleton scope & Singleton Design pattern
  • How to stop a thread?
  • Interrupting a thread in java
  • What is ThreadLocal in Java ?
  • ArrayList custom Implementation
  • Difference between volatile and synchronized keyword?
  • How to write thread safe code?
  • How to avoid deadlock in java ?

Filed Under: Core Java

Reader Interactions

Comments

  1. Hemant says

    July 18, 2012 at 2:46 pm

    Thanks, It’s very simple and valuable explanation.

    Reply

Leave a Reply Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

FOLLOW US ONLINE

  • View J2eereference-166104970118637’s profile on Facebook
  • View j2eereference’s profile on Twitter
  • View j2eereference’s profile on LinkedIn

Subscribe by email

Recent posts

  • Java Buzzwords
  • Anonymous Inner Class in Java
  • Network Programming – java.net Package
  • Java Regular Expressions
  • Method Local Inner Class in Java
  • URL Processing in Java
  • Iterator Design Pattern Implementation using Java
  • Strategy Design Pattern Implementation using Java
  • Decorator Design Pattern
  • Adapter Design Pattern Implementation using Java
  • JSF Composite Components
  • JSF UI Components
  • What is JavaServer Faces (JSF)?
  • GOF Design Patterns
  • History and Need for Design Patterns

Footer

Core Java
Design Patterns
JSP
Servlets
HTML
Building Tools
AJAX
SCJP
jQuery
Testing
Spring
UML
Struts
Java Centers
Java Training
Home
About Us
Contact Us
Copyright © j2eereference.com. All right reserved.