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?
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
- Difference between Types.INTEGER and Types.NULL in
static
fields are ignored for serialization.Updated to say
static
rather thantransient
as I originally intended...The short rules can be as follows:
1.
static
variable are not saved during serialization. And on the contrary, during de-serialization process, thestatic
variables are initiated from the class level initialization.2.
static
andtransient
keywords based variables are both ignored during serialization.3. Class name and
serialVersionUID
are both serialized as stream of bytes and when de-serialized theserialVersionUID
, read from the source, is compared with local class samestatic
variable. That is whyserialVersionUID
is declared asstatic public final
so that no further object needs to be created for comparing these versionUID(s)."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/
static
s are implicitlytransient
, 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.static
fields aren't serialized.Static fields can never be a part of "ser" file. Static + transient is the same as transient.