Java static serialization rules?

27,194

Solution 1

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.

Solution 2

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.

Solution 3

static fields are ignored for serialization.

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

Solution 4

static fields aren't serialized.

Share:
27,194
ahodder
Author by

ahodder

I'm a derp who derps with herps in hopes that the herp does all the derping I want it to.

Updated on July 09, 2022

Comments

  • ahodder
    ahodder almost 2 years

    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?

  • ahodder
    ahodder almost 13 years
    Thanks fro your response, I am aware however. Will the statics return, though, when the class is reinflated from the serialization process?
  • ahodder
    ahodder almost 13 years
    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?
  • ColinD
    ColinD almost 13 years
    @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.
  • Xairoo
    Xairoo almost 13 years
    The static variables exist as soon as the class is loaded since they are part of the class definition.
  • Steve Kuo
    Steve Kuo almost 13 years
    "Statics are transient by default", does this mean there's a way to make them not transient?
  • Xairoo
    Xairoo almost 13 years
    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".
  • Brett Kail
    Brett Kail almost 13 years
    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).
  • user207421
    user207421 almost 13 years
    Upvoted the pointless downvote. Answer is perfectly correct. The OP however is really asking about static, not transient.
  • Brett Kail
    Brett Kail almost 13 years
    Ugh! I clearly meant to say "static", and I couldn't read my own mistake even with the the downvote. Thanks to all ;).
  • Xairoo
    Xairoo almost 13 years
    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.
  • Brett Kail
    Brett Kail almost 13 years
    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 :-).
  • user207421
    user207421 almost 13 years
    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.
  • Cris
    Cris almost 13 years
    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 ?
  • user207421
    user207421 almost 13 years
    @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.
  • Cris
    Cris almost 13 years
    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....
  • user207421
    user207421 almost 13 years
    @Cris No, static variables are not serialized. I don't understand anything you've said here that starts with a 2.
  • Cris
    Cris almost 13 years
    "static variables are serialized ?"...should have been the question
  • Cris
    Cris almost 13 years
    Ok... i do not really have time now for this . I updated my comment to the main question because it was not very clear.
  • user207421
    user207421 almost 13 years
    @Cris still confused. Your first sentence is double-talk. Remove either 'non-static' or 'instance' and you are done.
  • Cris
    Cris almost 13 years
  • user207421
    user207421 almost 13 years
    @Cris doesn't stop it being double-talk ;-) 'Non-static' implies 'instance'.
  • Arunkumar Srisailapathi
    Arunkumar Srisailapathi about 9 years
    Although the static field serialVersionUID is serialized :) I know it is a pretty older post but I thought it should be good to add :)
  • Monish Kamble
    Monish Kamble almost 3 years
    @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?