
- 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
How transient works with final in Java serialization?
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).
Transient variables − The values of the transient variables are never considered (they are excluded from the serialization process). i.e. When we declare a variable transient, after de-serialization its value will always be null, false, or, zero (default value).
Therefore, 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
Example
In the following java program, the class Student has two instance variables name and age where, age is declared transient. In another class named SerializeExample 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.
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 void display() { System.out.println("Name: "+this.name); System.out.println("Age: "+this.age); } } public class SerializeExample { public static void main(String args[]) throws Exception { //Creating a Student object Student std = new Student("Sarmista", 27); //Serializing the object FileOutputStream fos = new FileOutputStream("e:\student.ser"); ObjectOutputStream oos = new ObjectOutputStream(fos); oos.writeObject(std); oos.close(); fos.close(); System.out.println("Values before de-serialization: "); std.display(); System.out.println("Object serialized......."); //De-serializing the object FileInputStream fis = new FileInputStream("e:\student.ser"); ObjectInputStream ois = new ObjectInputStream(fis); Student deSerializedStd = (Student) ois.readObject(); System.out.println("Object de-serialized......."); ois.close(); fis.close(); System.out.println("Values after de-serialization"); deSerializedStd.display(); } }
Output
Values before de-serialization: Name: Sarmista Age: 27 Object serialized....... Object de-serialized....... Values after de-serialization Name: Sarmista Age: 0
- Related Articles
- Object Serialization with inheritance in Java
- transient Keyword in Java
- Object Serialization with Inheritance in Java Programming
- How to implement custom JSON serialization with Gson in Java?
- How to implement custom JSON de-serialization with Gson in Java?\n
- What is Serialization in Java?
- Object Graph in Java Serialization
- What are Transient variables in Java? Explain.
- Difference between volatile and transient in java
- What does the modifier transient in Java do?
- Difference between Serialization and Externalization in Java
- How to exclude a field in Gson during serialization in Java?
- Why the transient variable is not serialized in Java?
- How can we ignore the fields during JSON serialization in Java?
- final keyword in Java
