What is transient variable inwards Java
transient variable inwards Java is a variable whose value is non serialized during Serialization together with which is initialized past times its default value during de-serialization, for representative for object transient variable it would hold upwardly null. this behaviour tin laissez passer on the sack hold upwardly customized past times using Custom Serialized shape or past times using Externalizable interface. transient variable is used to foreclose whatsoever object from beingness serialized together with y'all tin laissez passer on the sack brand whatsoever variable transient past times using transient keyword. By the means difference betwixt transient together with volatile variable inwards Java is a famous Java interview question exactly transient variable is completely unlike than volatile variable which nosotros convey discussed inwards our postal service What is volatile variable inwards Java. In side past times side department nosotros volition run into consummate representative of serialization where nosotros volition origin serialize an instance of Book course of teaching which implements Serializable together with than de-serialize to run into What is the value of transient variable later deserialization.
How to travel transient variable inwards Java - Serialization Example

package test;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
/**
*
* Java programme to demonstrate What is transient variable inwards Java together with fact that value of
* transient variable is non serialized together with during serialization it initialized amongst
* default value of that information type. e.g. If transient variable is Object than after
* deserialization its value would hold upwardly null.
*
* @author Javin
*/
public class TransientTest {
public static void main(String args[]) {
Book narnia = new Book(1024, "Narnia", "unknown", 2);
System.out.println("Before Serialization: " + narnia);
try {
FileOutputStream fos = new FileOutputStream("narnia.ser");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(narnia);
System.out.println("Book is successfully Serialized ");
FileInputStream fis = new FileInputStream("narnia.ser");
ObjectInputStream ois = new ObjectInputStream(fis);
Book oldNarnia = (Book) ois.readObject();
System.out.println("Book successfully created from Serialized data");
System.out.println("Book later seriazliation : " + oldNarnia);
} catch (Exception e) {
e.printStackTrace();
}
}
}
/*
* Influenza A virus subtype H5N1 course of teaching which implements Serializable interface together with has a transient variable.
*/
class Book implements Serializable{
private int ISBN;
private String title;
private String author;
private transient int edition = 1; //transient variable non serialized
public Book(int ISBN, String title, String author, int edition) {
this.ISBN = ISBN;
this.title = title;
this.author = author;
this.edition = edition;
}
@Override
public String toString() {
return "Book{" + "ISBN=" + ISBN + ", title="What is transient variable inwards Java - Serialization Example"color: blue;">", author=" + writer + ", edition=" + edition + '}';
}
}
Output:
Before Serialization: Book{ISBN=1024, title=Narnia, author=unknown, edition=2}
Book is successfully Serialized
Book successfully created from Serialized data
Book later seriazliation : Book{ISBN=1024, title=Narnia, author=unknown, edition=0}
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
/**
*
* Java programme to demonstrate What is transient variable inwards Java together with fact that value of
* transient variable is non serialized together with during serialization it initialized amongst
* default value of that information type. e.g. If transient variable is Object than after
* deserialization its value would hold upwardly null.
*
* @author Javin
*/
public class TransientTest {
public static void main(String args[]) {
Book narnia = new Book(1024, "Narnia", "unknown", 2);
System.out.println("Before Serialization: " + narnia);
try {
FileOutputStream fos = new FileOutputStream("narnia.ser");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(narnia);
System.out.println("Book is successfully Serialized ");
FileInputStream fis = new FileInputStream("narnia.ser");
ObjectInputStream ois = new ObjectInputStream(fis);
Book oldNarnia = (Book) ois.readObject();
System.out.println("Book successfully created from Serialized data");
System.out.println("Book later seriazliation : " + oldNarnia);
} catch (Exception e) {
e.printStackTrace();
}
}
}
/*
* Influenza A virus subtype H5N1 course of teaching which implements Serializable interface together with has a transient variable.
*/
class Book implements Serializable{
private int ISBN;
private String title;
private String author;
private transient int edition = 1; //transient variable non serialized
public Book(int ISBN, String title, String author, int edition) {
this.ISBN = ISBN;
this.title = title;
this.author = author;
this.edition = edition;
}
@Override
public String toString() {
return "Book{" + "ISBN=" + ISBN + ", title="What is transient variable inwards Java - Serialization Example"color: blue;">", author=" + writer + ", edition=" + edition + '}';
}
}
Output:
Before Serialization: Book{ISBN=1024, title=Narnia, author=unknown, edition=2}
Book is successfully Serialized
Book successfully created from Serialized data
Book later seriazliation : Book{ISBN=1024, title=Narnia, author=unknown, edition=0}
If y'all await at this representative of Serializing Object inwards Java y'all volition realize that value of transient variables are non serialized together with persisted together with during deserialization those value are initialized amongst at that spot default value which is null inwards instance of int variable. Since constructor too didn't run during de-serialization it won't larn the value provided during constructor. In Summary travel transient variable carefully inwards Java.
Further Learning
Complete Java Masterclass
Top x Serialization interview query inwards Java
No comments:
Post a Comment