Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Serialization

 
Allen Bandela
Ranch Hand
Posts: 128
Eclipse IDE MS IE Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there!
I wonder what exactly happens to a static variable that has been declared and initialized in a class that implements Serializable and when that class is passed to an output stream with the writeObject() method as follows:


Output: 10 0 10

As static variables would not be serialized, should'nt s2.z return 0? What exactly is happening here. Can someone explain?
 
wise owen
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Run B.class after runing A.class. You will get s2.z=9. because z is static member and will initialized when the class is loaded (first time).
 
James Quinton
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
don't get confused by transient.
after being deserialized, transient variable is reset to its default value, while static variable keeps its original value
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic