☰
|
B
|
Basic Level Interview Questions |
|
I
|
Intermediate Level Interview Questions |
|
A
|
Advanced Level Interview Questions |
When you want to store the transient variables state as a part of the serialized object at the time of serialization the class must implement the following methods:
private void wrtiteObject(ObjectOutputStream outStream) {
//code to save the transient variables state
//as a part of serialized object
}
private void readObject(ObjectInputStream inStream) {
//code to read the transient variables state
//and assign it to the de-serialized object
}
Externalizable provides us writeExternal() and readExternal() method which gives us flexibility to control java serialization mechanism instead of relying on Java's default serialization. Correct implementation of Externalizable interface can improve performance of application drastically.
serialVersionUID is used for version control of object. serialVersionUID is used to ensure that same class(That was used during Serialization) is loaded during Deserialization.
ANY-ACCESS-MODIFIER static final long serialVersionUID = 42L;
serialVersionUID must be Static and final. You can assign any number to it. For example, if you serialize an object and deserialize it, it will work fine as long as serialVersionUID is not changed. But after serializing, if you change serialVersionUID, and try to deserialize it, you will get error. Something like this:
local class incompatible: stream classdesc serialVersionUID = 1, local class serialVersionUID = 2
It complains about Serialvesionuid being changed.
It is possible that you have serialized an object in a file and you deserialized it after few months on different JVM. In between serialization and deserialization class declaration has been changed. So it is a good idea to maintain version system and serialversionuid does exactly same thing. It checks if you are deserializing same object which you have serialized.
The value of the serialVersionUID field should ideally be changed when incompatible changes are made to the structure of the class.
Changes to a serializable class can be compatible or incompatible. Following is the list of changes which are compatible:
List of incompatible changes:
Declaring an explicit serialVersionUID field in your classes saves some CPU time only the first time the JVM process serializes a given Class. However the gain is not significant, In case when you have not declared the serialVersionUID its value is computed by JVM once and subsequently kept in a soft cache for future use.
It depends on whether class has its own serialVersionUID or not. As we know that if we don't provide serialVersionUID in our code java compiler will generate it and normally it’s equal to hashCode of object. By adding any new field there is chance that new serialVersionUID generated for that class version is not the same of already serialized object and in this case Java Serialization API will throw java.io.InvalidClassException and this is the reason its recommended to have your own serialVersionUID in code and make sure to keep it same always for a single class.
When an object is serialized, the serialVersionUID is serialized along with the other contents. Later when that is deserialized, the serialVersionUID from the deserialized object is extracted and compared with the serialVersionUID of the loaded class. If the numbers do not match then, InvalidClassException is thrown.
If the loaded class is not having a serialVersionUID declared, then it is automatically generated.
serialVersionUID is a 64-bit hash of the class name, interface class names, methods and fields. Serialization runtime generates a serialVersionUID if you do not add one in source.
However, it is strongly recommended that all serializable classes explicitly declare serialVersionUID values, since the default serialVersionUID computation is highly sensitive to class details that may vary depending on compiler implementations and can produce different serialVersionUID in different environments. This can result in unexpected InvalidClassException during de-serialization.
Refer this link for the algorithm to generate serialVersionUID.
You can use JDK “serialver” or Eclipse IDE to generate serialVersionUID automatically.