aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Statics & Serialization Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Statics & Serialization" Watch "Statics & Serialization" New topic
Author

Statics & Serialization

Santiago Bravo
Ranch Hand

Joined: Jul 25, 2008
Posts: 226
Hi All,

K&B (SCJP1.5) page 456 says that serialization is not for statics. I understand that statics are class variables hence do not get saved during serialization. So that led me to try and find out what happens when an object which has a static member is saved. What happens when the object is saved then read back?

I tried to solve the above with the folowing code:

====================================================

import java.io.*;

public class TestStatic implements Serializable{

public static int x = 0;

public TestStatic(){
x = 1;
}

public static void setTestStatic(){
x = 2;
}

public static void main(String[] args) throws Throwable{

TestStatic W = new TestStatic();//x is 1
W.setTestStatic(); // x is 2

File f = new File("a.txt");

ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(f));
oos.writeObject(W);
oos.close();

ObjectInputStream ois = new ObjectInputStream(new FileInputStream(f));
TestStatic A = (TestStatic)ois.readObject();
System.out.println(A.x);
ois.close();

}

}

====================================================

This gives the output "2" which is what the static variable was just before it was saved. I thought maybe staic variables get thier initial values like transient variables but this is not the case.


Thanks

Can anyone explain what happens to static variables when they are saved and then read back?


Thanks


Santiago
My Path to SCJP Certification My Path to SCWCD Certification
Rudolf Meerkotter
Greenhorn

Joined: Apr 03, 2008
Posts: 14
Hi Santiago,

I think it's because the compiler replaces A.x with TestStatic.x, which is not read from the deserialized object, but associated with the class.

Best,
Rudolf


SCJP 5, SCWCD 5, SCBCD 5
Aravind Jerubandi
Ranch Hand

Joined: Dec 17, 2006
Posts: 49
The static variables are stored at the class level. They are not tied to any object. And we can serialize only the Object. So, technically static variables can not be serialized. This means that the static variable values doesn't get serialized. In the deserialized object the value of the static variable will always be the latest value of that variable. The value the variable had, when serializing the object will not persist.

Just after the below code you try modifying the value of the static variable:


Now when you deserialize the value of the variable will be 1000 and not 1, which was the value when it was serialized. This won't be the case with instance variables.

Hope this helps...


-Aravind<br />SCJP 5.0 (91%), Preparing for SCBCD.
Santiago Bravo
Ranch Hand

Joined: Jul 25, 2008
Posts: 226
Originally posted by Aravind Jerubandi:
[QB] In the deserialized object the value of the static variable will always be the latest value of that variable. The value the variable had, when serializing the object will not persist.[QB]


Makes perfect sense now, thanks!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Statics & Serialization