< Prev
Next >

Static in Serialization

What are Static members and who do they belong to?

Static members/variables belong to the class and not the object. Hence, static members are called - "class members" and not instance/object variables.

Why the values of static members are not saved when an object is Serialized?

Static members are never part of an object and hence, when an object's state is saved by the process of Serialization, the values of static members of a class are not saved and static members are not a part of the saved state of an object.


Let's understand this by an example -:


class StaticSerial implements Serializable
static int i =100;	// static int(class member), belong to the class and not object
public static void main(String... ar)
StaticSerial ob= new StaticSerial();
System.out.println("At the time of Serialization, static member has value : " + i);
	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
	i=99;	// static member's value is changed to 99

	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.
	System.out.println("After Deserialization, static member has value : "+ i);
catch(Exception e)

} //main method ends
} //class definition ends

Output is -:

At the time of Serialization, static member has value : 100
After Deserialization, static member has value : 99

Program Analysis

Please share this article -

< Prev
Next >
< Transient Keyword
What is Thread? >


Please Subscribe

Please subscribe to our social media channels for daily updates.

Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page



Please check our latest addition