Home » Core Java » static and transient variable in Serialization

static and transient variable in Serialization

serialVersionUID :

The serialVersionUID is used as a version control in a Serializable class.

Explicit serialVersionUID can be declared by 2 ways 

1 : default serial version ID :

One can use this option to add a user-defined ID in combination with custom serialization code if the type did undergo structural changes since its first release.

2 : generatedserial version ID :

One can use this option to add a compiler-generated ID if the type did not undergo structural changes since its first release.

default serial version ID  vs generated serial version ID :

1 : serialVersionUID is used with static keyword ,so it  does not get serialized. During serialization ObjectOutputStream writes every time the value of serialVersionUID to the output stream. During deserialization ObjectInputStream reads it back and if the value read from the stream does not match with the serialVersionUID value in the current version of the class, then we get InvalidClassException.

2 : If there is no serialVersionUID declared in the serialized class , compiler automatically adds it with a value generated based on the fields declared in the class.So the serial version ID is compiler generated.

3 : If the variables,methods and properties of the class is going to change in future ,the compiler generated serial version id takes care of these changes.If you use default serial version id,you need to keep track of these changes.

4 : default serial version ID may not be unique always in a  big Java application.During deserialization it can give exceptions.

In my opinion use compiler generated serial version ID.

transient Variables :

A variable defined with transient keyword is not serialized during serialization process.This variable will be initialized with default value during deserialization. (e.g: for objects  it is null, for int it is 0).

static Variables :

A variable defined with static keyword is not serialized during serialization process.This variable will be loaded with current value defined in the class during deserialization.

Instance Variables:

These variables are serialized during serialization process , so after deserialization one will get back the serialized state.

Super class variables:

If super class also implements 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.

Example :

In the below program ,

static variable empId is initialized to “XYZ123”  ,So after deserialization it will have its current  value or state  i.e. “XYZ123”.

transient variable name is assigned “Ajay” value while creating employee object.Since it will not be serialized,so after deserialization it will have String default value null.

instance variable department  is assigned “Mortgage” value while creating employee object.Since it will be serialized,so after deserialization it will have same value or state “Mortgage”.

 

Download Employee.txt

 


Leave a comment

Your email address will not be published. Required fields are marked *

nine + 16 =