• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Why Serialization ignores static fields?

 
Ranch Hand
Posts: 159
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why Serialization ignores static fields?
 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The point of serialization is to save the state of an object, that is, an instance of the class. Static fields do not belong to any instance of the class, they refer to the class itself, so saving and then reloading them for every given object would be no sense anyway.
 
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
After deserialization if i want to retain the value of Static Fields, what should we do?
 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

After deserialization if i want to retain the value of Static Fields, what should we do?



Stick them in instance variables :-)
 
Sergey Petunin
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can do nothing. Static fields are absolutely independent of the serialization, because they don't belong to the instance object.
[ December 17, 2007: Message edited by: Serge Petunin ]
 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Thirugnanam Saravanan:
After deserialization if i want to retain the value of Static Fields, what should we do?



You should search for a tutorial on handling serialization requests manually.

In short, you must implement the methods, writeObject and readObject. After writing the objects to the stream, you can simply write your static fields to the stream. While reading, read the object first and then the static variables and initialize them as such, assuming that your static variables can be initialized at runtime. For reference, code snippet.
 
reply
    Bookmark Topic Watch Topic
  • New Topic