41

I'm working on a save state serialization with a few static methods and fields. I could have sworn though that serialization and static's caused mayhem. Should I make all static's transient? And will inflating the calls restore the statics as normal?

ColinD
  • 103,631
  • 27
  • 195
  • 199
ahodder
  • 11,060
  • 14
  • 63
  • 109

6 Answers6

74

statics are implicitly transient, so you don't need to declare them as such.

Serialization is for serializing instances, not classes. static fields (methods are irrelevant since they are part of the class definition so they aren't serialized) will be reinitialized to whatever value they are set to when the class is loaded.

If you have a mutable static field, then the changes made to that value will be lost.

roottraveller
  • 6,328
  • 4
  • 50
  • 59
Robin
  • 23,156
  • 4
  • 47
  • 57
  • Ah I see, I didn't know that statics where inheriently transient. So, how would I ensure the the reienflated instance will still be able to access static's? – ahodder Jun 21 '11 at 17:36
  • The static variables exist as soon as the class is loaded since they are part of the class definition. – Robin Jun 21 '11 at 17:39
  • "Statics are transient by default", does this mean there's a way to make them not transient? – Steve Kuo Jun 21 '11 at 17:55
  • Changed the wording to "implicitly transient". If you customize the serialization you could effectively make them non transient, but it cannot be done "out of the box". – Robin Jun 21 '11 at 18:17
9

The short rules can be as follows:

1. static variable are not saved during serialization. And on the contrary, during de-serialization process, the static variables are initiated from the class level initialization.

2. static and transient keywords based variables are both ignored during serialization.

3. Class name and serialVersionUID are both serialized as stream of bytes and when de-serialized the serialVersionUID , read from the source, is compared with local class same static variable. That is why serialVersionUID is declared as static public final so that no further object needs to be created for comparing these versionUID(s).

  • If in case any difference is found, then a InvalidClassException would occur.
roottraveller
  • 6,328
  • 4
  • 50
  • 59
Gagandeep
  • 145
  • 1
  • 7
3

static fields are ignored for serialization.

Updated to say static rather than transient as I originally intended...

roottraveller
  • 6,328
  • 4
  • 50
  • 59
Brett Kail
  • 32,618
  • 2
  • 82
  • 87
  • Thanks fro your response, I am aware however. Will the statics return, though, when the class is reinflated from the serialization process? – ahodder Jun 21 '11 at 17:34
  • 1
    @Aedon: The static fields for the class will continue to be whatever they were already set to in the VM where the object is deserialized. If the class is initialized for the first time when the object is deserialized, the static fields will be in their initial state. – ColinD Jun 21 '11 at 17:37
  • Downvote with no explanation? Unfortunate... Well, as ColinD reiterated, statics are ignored for serialization (they are neither written to nor read from a serialization stream). – Brett Kail Jun 21 '11 at 18:29
  • Upvoted the pointless downvote. Answer is perfectly correct. The OP however is really asking about static, not transient. – user207421 Jun 22 '11 at 05:52
  • Ugh! I clearly meant to say "static", and I couldn't read my own mistake even with the the downvote. Thanks to all ;). – Brett Kail Jun 22 '11 at 06:12
  • The original downvote was for not actually answering the question, and I didn't comment since I thought it was obvious. Anyway, removed my downvote since your edit. – Robin Jun 22 '11 at 14:18
  • Thanks. FWIW, I think your downvote was appropriate, but a comment is always useful for the same reason code reviews are useful: sometimes you can't see your own embarrassing mistakes until someone shows them to you :-). – Brett Kail Jun 22 '11 at 15:21
1

static fields aren't serialized.

roottraveller
  • 6,328
  • 4
  • 50
  • 59
ColinD
  • 103,631
  • 27
  • 195
  • 199
  • 1
    Although the static field serialVersionUID is serialized :) I know it is a pretty older post but I thought it should be good to add :) – Arunkumar Srisailapathi Mar 13 '15 at 11:07
  • @Arunkumar Srisailapathi I came to look for a reason behind the same thing you commented. Could you help to explain how that works as by specification static fields are not serialized, is serialVersionUID an exception to this? – Monish Kamble May 26 '21 at 19:56
0

Static fields can never be a part of "ser" file. Static + transient is the same as transient.

Jatin Shashoo
  • 499
  • 6
  • 17
0

"When you serialize an instance of a class, the only things that are saved are the non-static and non-transient instance data. Class definitions are not saved. They must be available when you try to deserialize an object" http://java.sun.com/developer/technicalArticles/ALT/serialization/

Cris
  • 4,779
  • 5
  • 38
  • 73
  • Short but very confused answer. Static variables are not *serialized.* Serializability has nothing to do with it. 'Default' means nothing here, and the second sentence is completely meaningless. – user207421 Jun 22 '11 at 23:50
  • so please answer to me with yes or no :1)static variables are serializable ? 2) static variable are bound to and instance ,particular object ? (as far as we all know are bound to class)...so what the ... hell is wrong here ? – Cris Jun 23 '11 at 06:29
  • @Cris (1) basically meaningless. As static variables aren't serialized the question of whether they are serializable doesn't arise, but if it did it would depend entirely on the actual type of the referenced object. Your question (2) is completely meaningless. – user207421 Jun 23 '11 at 10:45
  • static variables are serialized...should have been the question.being non english native...i put serializable. 2) it has because in the context of the question it is a reason why static variable are not serialized.... – Cris Jun 23 '11 at 11:16
  • @Cris No, static variables are *not* serialized. I don't understand anything you've said here that starts with a 2. – user207421 Jun 23 '11 at 11:29
  • "static variables are serialized ?"...should have been the question – Cris Jun 23 '11 at 12:46
  • Ok... i do not really have time now for this . I updated my comment to the main question because it was not very clear. – Cris Jun 23 '11 at 12:48
  • @Cris still confused. Your first sentence is double-talk. Remove either 'non-static' or 'instance' and you are done. – user207421 Jun 25 '11 at 10:09
  • It it a copy from http://java.sun.com/developer/technicalArticles/ALT/serialization/ – Cris Jun 25 '11 at 12:01
  • @Cris doesn't stop it being double-talk ;-) 'Non-static' implies 'instance'. – user207421 Jun 26 '11 at 10:24