Advertisement
false | |
zero | |
0.0 | |
'\u0000' | |
null |
Advertisement
//Java - Example of transient keyword
import java.io.*;
//A class implementing Serializable interface
class TransientExm implements Serializable
{
transient int i=10; //instance variable of primitive int type is marked "transient"
public static void main(String... ar)
{
TransientExm ob= new TransientExm();
System.out.println("Before Serialization object has value : " + ob.i);
try
{
//Writing saved state of the object in this file
FileOutputStream fos= new FileOutputStream("E:\\File.ser");
ObjectOutputStream oos= new ObjectOutputStream(fos);
//Serializing, i.e. saving the state of this object, ob
oos.writeObject(ob);
oos.close();
}
catch(Exception exp)
{
System.out.println(exp);
}
try
{
//Reading the same file for Deserialization
FileInputStream fis= new FileInputStream("E:\\File.ser");
ObjectInputStream ois= new ObjectInputStream(fis);
//Deserializing, i.e. restoring the object to its original state.
ob=(TransientExm)ois.readObject();
ois.close();
System.out.println("After Deserialization restored object has value : "+ ob.i);
}
catch(Exception exp)
{
System.out.println(exp);
}
}
}
Before Serialization object has value : 10
After Deserialization restored object has value : 0
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement