Java static serialization rules?
Solution 1
static
s 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.
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, 2022Comments
-
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 almost 13 yearsThanks fro your response, I am aware however. Will the statics return, though, when the class is reinflated from the serialization process?
-
ahodder almost 13 yearsAh 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 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 almost 13 yearsThe static variables exist as soon as the class is loaded since they are part of the class definition.
-
Steve Kuo almost 13 years"Statics are transient by default", does this mean there's a way to make them not transient?
-
Xairoo almost 13 yearsChanged 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 almost 13 yearsDownvote 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 almost 13 yearsUpvoted the pointless downvote. Answer is perfectly correct. The OP however is really asking about static, not transient.
-
Brett Kail almost 13 yearsUgh! I clearly meant to say "static", and I couldn't read my own mistake even with the the downvote. Thanks to all ;).
-
Xairoo almost 13 yearsThe 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 almost 13 yearsThanks. 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 almost 13 yearsShort 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 almost 13 yearsso 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 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 almost 13 yearsstatic 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 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 almost 13 years"static variables are serialized ?"...should have been the question
-
Cris almost 13 yearsOk... i do not really have time now for this . I updated my comment to the main question because it was not very clear.
-
user207421 almost 13 years@Cris still confused. Your first sentence is double-talk. Remove either 'non-static' or 'instance' and you are done.
-
Cris almost 13 yearsIt it a copy from java.sun.com/developer/technicalArticles/ALT/serialization
-
user207421 almost 13 years@Cris doesn't stop it being double-talk ;-) 'Non-static' implies 'instance'.
-
Arunkumar Srisailapathi about 9 yearsAlthough 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 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?