
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
Why the transient variable is not serialized in Java?
The Serialization is a process to persists java objects in the form of a sequence of bytes that includes the object’s data as well as information about the object’s type and the types of data stored in the object. The Serialization is the translation of Java object’s values/states to bytes to send it over the network or to save it. On the other hand, the Deserialization is the conversion of byte code to corresponding java objects.
The Transient variable is a variable whose value is not serialized during the serialization process. We will get a default value for this variable when we deserialize it.
Syntax
private transient <member-variable>;
Example
import java.io.*; class EmpInfo implements Serializable { String name; private transient int age; String occupation; public EmpInfo(String name, int age, String occupation) { this.name = name; this.age = age; this.occupation = occupation; } public String toString() { StringBuffer sb = new StringBuffer(); sb.app*end("Name:"+"\n"); sb.append(this.name+"\n"); sb.append("Age:"+ "\n"); sb.append(this.age + "\n"); sb.append("Occupation:" + "\n"); sb.append(this.occupation); return sb.toString(); } } // main class public class TransientVarTest { public static void main(String args[]) throws Exception { EmpInfo empInfo = new EmpInfo("Adithya", 30, "Java Developer"); ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("empInfo")); oos.writeObject(empInfo); oos.close(); ObjectInputStream ois = new ObjectInputStream(new FileInputStream("empInfo")); EmpInfo empInfo1 = (EmpInfo)ois.readObject(); System.out.println(empInfo1); } }
Output
Name: Adithya Age: 0 Occupation: Java Developer
- Related Articles
- transient Keyword in Java
- What is the difference between transient and volatile in Java?
- What does the modifier transient in Java do?
- Are the values of static variables stored when an object is serialized in Java?
- What are Transient variables in Java? Explain.
- Difference between volatile and transient in java
- Are the constructors in an object invoked when de-serialized in Java?
- How transient works with final in Java serialization?
- Why multiple inheritance is not supported in Java
- Why multiple inheritance is not supported by Java?
- Why is operator overloading not supported by java?
- What is variable shadowing in java?
- Variable in subclass hides the variable in the super class in Java
- What is a final variable in java?
- What is JAVA_HOME variable in Java Environment?

Advertisements