Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

Statics & Serialization

 
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...
 
Santiago Bravo
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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!
reply
    Bookmark Topic Watch Topic
  • New Topic