Advertisement
Advertisement
//Java - Static in Serialization Example
import java.io.*;
class StaticSerial implements Serializable
{
// static int(class member), belong to the class and not object
static int i =100;
public static void main(String... ar)
{
StaticSerial ob= new StaticSerial();
System.out.println("At the time of Serialization, static member has value : " + i);
try
{
//Serialization
FileOutputStream fos= new FileOutputStream("E:\\File.ser"); //Writing saved state of the object in this file
ObjectOutputStream oos= new ObjectOutputStream(fos);
oos.writeObject(ob); //Serializing, i.e. saving the state of this object, ob
oos.close();
//The value of static member is changed to 99
i=99;
//Deserialization
FileInputStream fis= new FileInputStream("E:\\File.ser"); //Reading the same file for Deserialization
ObjectInputStream ois= new ObjectInputStream(fis);
ob=(StaticSerial)ois.readObject(); //Deserializing, i.e. restoring the object to its original state.
ois.close();
System.out.println("After Deserialization, static member has value : "+ i);
}
catch(Exception e)
{
System.out.println(e);
}
} //main method ends
} //class definition ends
At the time of Serialization, static member has value : 100
After Deserialization, static member has value : 99
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement