| 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.
|
 |
 |
|
|
subject: Why Serialization ignores static fields?
|
|
|