• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

value of a static variable on being serialized

 
Alicia Logan
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Suppose i have a class with a static variable i = 10. Now i serialize this class and write the object of this class to a file. What will be value of i?
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try it and see.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The value of the static variable won't be serialized. When the object is deserialized into a new JVM, assuming the class hadn't already been loaded, the class will first be loaded normally. That means that any static initializers will be executed, so that the value of that static variables will be whatever it is right after the class is loaded. If the class had already been loaded, then the static variable won't be affected by the object being deserialized.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic