Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are static variables serialized in Serialization process

Tags:

java

I'm stumbled upon understanding java serialization. I have read in many documents and books that static and transient variables cannot be serialized in Java. We declare a serialVersionUid as follows.

private static final long serialVersionUID = 1L; 

If a static variable was not serialized then, we often face an exception during the de-serialization process.

java.io.InvalidClassException 

in which the serialVersionUID from the deserialized object is extracted and compared with the serialVersionUID of the loaded class.

To my knowledge i think that if static variables cannot be serialized. There is no point of that exception. I may be wrong because I'm still learning.

Is it a myth that "Static and transient variables in java cannot be serialized". Please correct me, I'm in a mess about this concept.

like image 654
srk Avatar asked Jun 12 '12 16:06

srk


People also ask

What happens to static variables during serialization?

In Java, serialization is a concept using which we can write the state of an object into a byte stream so that we can transfer it over the network (using technologies like JPA and RMI). But, static variables belong to class therefore, you cannot serialize static variables in Java.

Is it static member variables are serialized?

Static Variables: These variables are not serialized, So during deserialization static variable value will loaded from the class.

Do static fields get serialized?

static fields aren't serialized.

Which kind of variables is not serialized during Java serialization?

The Transient variable is a variable whose value is not serialized during the serialization process. We will get a default value for this variable when we deserialize it.


2 Answers

  1. Instance Variables: These variables are serialized, so during deserialization we will get back the serialized state.

  2. Static Variables: These variables are not serialized, So during deserialization static variable value will loaded from the class.(Current value will be loaded.)

  3. transient Variables: transient variables are not serialized, so during deserialization those variables will be initialized with corresponding default values (ex: for objects null, int 0).

  4. Super class variables: If super class also implemented Serializable interface then those variables will be serialized, otherwise it won't serialize the super class variables. and while deserializing, JVM will run default constructor in super class and populates the default values. Same thing will happen for all superclasses.

like image 133
Satya Avatar answered Oct 09 '22 00:10

Satya


serialVersionUID is a special static variable used by the serialization and deserialization process, to verify that a local class is compatible with the class used to serialize an object. It's not just a static variable as others, which are definitely not serialized.

When an object of a class is first serialized, a class descriptor containing among other things the class name and serial version UID is written to the stream. When this is deserialized, the JVM checks if the serial version UID read from the stream is the same as the one of the local class. If they're not, it doesn't even try to deserialize the object, because it knows the classes are incompatible.

like image 26
JB Nizet Avatar answered Oct 08 '22 22:10

JB Nizet