Java static serialization rules?
Asked Answered
S

7

43

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?

Sebiferous answered 21/6, 2011 at 17:30 Comment(0)
L
81

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.

Lepus answered 21/6, 2011 at 17:33 Comment(4)
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?Sebiferous
The static variables exist as soon as the class is loaded since they are part of the class definition.Lepus
"Statics are transient by default", does this mean there's a way to make them not transient?Swirly
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".Lepus
W
10

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.
Whiteside answered 21/7, 2016 at 6:49 Comment(0)
O
3

static fields are ignored for serialization.

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

Organzine answered 21/6, 2011 at 17:32 Comment(7)
Thanks fro your response, I am aware however. Will the statics return, though, when the class is reinflated from the serialization process?Sebiferous
@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.Winwaloe
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).Organzine
Upvoted the pointless downvote. Answer is perfectly correct. The OP however is really asking about static, not transient.Maskanonge
Ugh! I clearly meant to say "static", and I couldn't read my own mistake even with the the downvote. Thanks to all ;).Organzine
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.Lepus
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 :-).Organzine
W
1

static fields aren't serialized.

Winwaloe answered 21/6, 2011 at 17:34 Comment(3)
Although the static field serialVersionUID is serialized :) I know it is a pretty older post but I thought it should be good to add :)Loisloise
@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?Crusty
@MonishKamble A class descriptor is serialized, containing the name and the serialVersionUID.Maskanonge
A
1

Static belongs to class, Serialization deals with object/instance.

Thus declaring static, it doesn't belong to object anymore and cannot be serialized.

Arlinearlington answered 7/7, 2021 at 4:45 Comment(0)
D
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/

Daughter answered 21/6, 2011 at 17:35 Comment(10)
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.Maskanonge
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 ?Daughter
@Daughter (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.Maskanonge
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....Daughter
@Daughter No, static variables are not serialized. I don't understand anything you've said here that starts with a 2.Maskanonge
"static variables are serialized ?"...should have been the questionDaughter
Ok... i do not really have time now for this . I updated my comment to the main question because it was not very clear.Daughter
@Daughter still confused. Your first sentence is double-talk. Remove either 'non-static' or 'instance' and you are done.Maskanonge
It it a copy from java.sun.com/developer/technicalArticles/ALT/serializationDaughter
@Daughter doesn't stop it being double-talk ;-) 'Non-static' implies 'instance'.Maskanonge
T
0

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

Tiphany answered 2/9, 2018 at 9:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.