public class MySerializable implements Serializable{
private int x=10;
private static int y = 15;
public static void main(String...args){
AnotherClass a = new AnotherClass();
AnotherClass b;
//Serialize
try {
FileOutputStream fout = new FileOutputStream("MyFile.ser");
ObjectOutputStream Oout = new ObjectOutputStream(fout);
Oout.writeObject(a);
System.out.println( a.toString());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
//De-serialize
try {
FileInputStream fis = new FileInputStream("MyFile.ser");
ObjectInputStream Oin = new ObjectInputStream (fis);
b = (AnotherClass) Oin.readObject();
System.out.println( b.toString());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}catch (ClassNotFoundException e){
e.printStackTrace();
}
}
}
class AnotherClass implements Serializable{
transient int x = 8;
static int y = 9;
@Override
public String toString() {
return "x : " + x + ", y :" + y;
}
}
Can you please tell me how the static variable is serialized ??
Static variables − The values of static variables will not be preserved during the de-serialization process. In-fact static variables are also not serialized but since these belongs to the class.
Static variables are not serialized.
You can prevent member variables from being serialized by marking them with the NonSerialized attribute as follows. If possible, make an object that could contain security-sensitive data nonserializable. If the object must be serialized, apply the NonSerialized attribute to specific fields that store sensitive data.
Serialization allows the developer to save the state of an object and re-create it as needed, providing storage of objects as well as data exchange. Through serialization, a developer can perform actions such as: Sending the object to a remote application by using a web service.
Apparently, static variables can be serialized (But you should not do that), since serialization is the process of saving the state of an instance of a class, and static variables are common to all instances. They don't say anything about the instance's state, so, it wouldn't make sense at all.
Suppose you were allowed to serialize a static variable. Then, when you deserialize the instance, you will be getting an old copy of that variable, which might have been changed since then. Since the static variable is shared across all instances of the class, a change in the variable from any instance must be reflected in this instance.
So, they should not be serialized, because the variable under these conditions could possibly violate its contract as a static variable.
Serialization: -
Deserialization: -
The Current output of MySerializable Class is below
x : 8, y :9
x : 0, y :9
In this case the static variable are getting printed after calling toString() method, by this time it will reads value from class level variable.
Try this:
Add this line of code in MySerializable Class after //Serialize
block
AnotherClass.y = 5;
the output is :
x : 8, y :9
x : 0, y :5
this means the static variable is not storing in the file, it will read dynammically by toString() method.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With