Monday, 25 April 2016

Can we serialize static variables ?


Serialization
 is the process of converting a set of object instances that contain references to each other into a linear stream of bytes, which can then be sent through a socket, stored to a file, or simply manipulated as a stream of data.

Serialization is the mechanism used by RMI to pass objects between JVMs, either as arguments in a method invocation from a client to a server or as return values from a method invocation.

There are three exceptions in which serialization does not necessarily read and write to the stream.
 1. Serialization ignores static fields, because they are not part of any particular object's state.
 2. Base class fields are only handled if the base class itself is serializable.
 3. Transient fields.

There are four basic things you must do when you are making a class serializable : 
    1. Implement the Serializable interface.
    2. Make sure that instance-level, locally defined state is serialized properly.
    3. Make sure that superclass state is serialized properly.
    4. Override equals() and hashCode().

No comments:

Post a Comment

Note: only a member of this blog may post a comment.