Question from a mock exam: 1) Class fields with the following modifiers will not be serialized private static transient protected Answer provided : static, transient. Why static? I thought it was only transient. Is it because a static member belongs to the class and not to an instance and serialization is always done on objects?? Thanks Sharda
Imagine you have two objects of the same class that share a static variable. You serialize one of the objects. You then change the contents of the static variable. Now you deserialize the first object. What should the contents of the static variable be? The point is that static variables are associated with classes and not objects. But we serialize objects and not classes.
What if I wish to stop the execution of application completely today. And I want all objects to be saved as they are. Also all static variables to be saved as they are today. Tomorrow, I want to resume the application from same status. Would that require serializing static variables via Class object? Thanks Barkat
hi all, im preparing for sjcp2.. so wat is the right answer for the above mentioned question ?? is it only transient or both static and transient ??? Thanks, raj
SCJP, SCWCD, SCBCD, Oracle Certified Professional (SQL n PL/SQL)
import java.io.*; public class test implements java.io.Serializable{ static int i; int j; public static void main(String args[]) throws Exception{ ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(new File("txtfile"))); test t1 = new test(); t1.i=5; t1.j=10; out.writeObject((Object)t1); out.flush(); out.close(); ObjectInputStream in = new ObjectInputStream(new FileInputStream(new File("txtfile"))); //t1.i=2; //t1.j=5; test t = (test)in.readObject(); System.out.println(t.i); System.out.println(t.j); in.close(); } } Output: 5 10 If u remove comments and execute Output: 2 10 This means static field are serializable
The default deserialization mechanism for objects restores the contents of each field to the value and type it had when it was written. Fields declared as transient or static are ignored by the deserialization process.
The default serialization mechanism for an object writes the class of the object, the class signature, and the values of all non-transient and non-static fields.
So maybe what you see is not what you get? -Barry [ September 27, 2002: Message edited by: Barry Gaunt ]
Actually your code example proves that statics are not serializable.
If the static was serializable then the output would be 5 & 10 since that is what the variables looked like when the object was serialized. But the result is 2 & 10. The non-static variable is replaced and the static variable maintains the value it was changed to after the object was serialized. [ September 27, 2002: Message edited by: Thomas Paul ] [ September 27, 2002: Message edited by: Thomas Paul ]
Thomas Paul
mister krabs
Ranch Hand
Joined: May 05, 2000
Posts: 13974
posted
0
Originally posted by Barkat Mardhani: What if I wish to stop the execution of application completely today. And I want all objects to be saved as they are. Also all static variables to be saved as they are today.
I came across this old post; which I stumped into same question. Yes both static and transient fields are not serialized.
Test is - Serialize object. Stop JVM ; and run DeSerialize test; you see null values for static fields and transient field objects;
public class Test1 implements Serializable {
static String s;
}
Program 1
public class Program1{
public static void main(String[] args) throws Exception {
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("C:\\Desktop\\venu\\serial.test"));
Test1 t = new Test1();
t.s="Hello";
out.writeObject(t);
}
}
STOP JVM
Run this piece of code.
public class Program1{
public static void main(String[] args) throws Exception {
System.out.println(Test1.s); //before deserialization
ObjectInputStream in = new ObjectInputStream(new FileInputStream("C:\\Desktop\\venu\\serial.test"));
Test1 t = (Test1)in.readObject();
System.out.println(Test1.s); //After deserialization
}
}
========== string s value ====
null
null
=================