This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Why Serialization ignores static fields? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Why Serialization ignores static fields?" Watch "Why Serialization ignores static fields?" New topic
Author

Why Serialization ignores static fields?

rex tony
Ranch Hand

Joined: Aug 29, 2007
Posts: 159
Why Serialization ignores static fields?
Sergey Petunin
Ranch Hand

Joined: Dec 16, 2007
Posts: 44
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.
Thirugnanam Saravanan
Ranch Hand

Joined: Dec 13, 2007
Posts: 81
After deserialization if i want to retain the value of Static Fields, what should we do?


Saravanan
SCJP 5.0(98%), SCWCD 5.0 (100%), OCA
Panseer Kaur
Ranch Hand

Joined: Nov 01, 2007
Posts: 44
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

Joined: Dec 16, 2007
Posts: 44
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 ]
Gaurav Arora
Ranch Hand

Joined: Aug 13, 2007
Posts: 35
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why Serialization ignores static fields?