- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What are Transient variables in Java? Explain.
In Java, serialization is a concept using which we can write the state of an object into a byte stream so that we can transfer it over the network (using technologies like JPA and RMI).
While serializing an object of a class, if you want JVM to neglect a particular instance variable you need can declare it transient.
public transient int limit = 55; // will not persist public int b; // will persist
In the following java program, the class Student has two instance variables name and age where, age is declared transient. In another class named EampleSerialize we are trying to serialize and desterilize the Student object and display its instance variables. Since the age is made invisible (transient) only the name value is displayed.
Example
import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.Serializable; class Student implements Serializable{ private String name; private transient int age; public Student(String name, int age){ this.name = name; this.age = age; } public String getName() { return this.name; } public void setName(String name) { this.name = name; } public void setAge(int age) { this.age = age; } public int getAge() { return this.age; } } public class ExampleSerialize{ public static void main(String args[]) throws Exception{ Student std1 = new Student("Krishna", 30); FileOutputStream fos = new FileOutputStream("e:\student.ser"); ObjectOutputStream oos = new ObjectOutputStream(fos); oos.writeObject(std1); FileInputStream fis = new FileInputStream("e:\student.ser"); ObjectInputStream ois = new ObjectInputStream(fis); Student std2 = (Student) ois.readObject(); System.out.println(std2.getName()); } }
Output
Krishna
- Related Articles
- transient Keyword in Java
- What are class variables, instance variables and local variables in Java?
- What does the modifier transient in Java do?
- What is the difference between transient and volatile in Java?
- Difference between volatile and transient in java
- How transient works with final in Java serialization?
- What Environment Variables are required to set in Java Applications?
- What are variables?
- Why the transient variable is not serialized in Java?
- What are variables and types of variables in C++?
- What are local variables and global variables in C++?
- Why variables are declared final in Java
- What is blank final variable? What are Static blank final variables in Java?
- What are the variables?
- What are the types of variables a class can have in Java?

Advertisements