Why?
1. you want to store the state of your object, and shut down the jvm
1. you want to store the state of your object, and shut down the jvm
come after 20 days and recreate the same object live in exactly same state.
Save where?
a. file
b.db
c. send to any other apps via network.
a. file
b.db
c. send to any other apps via network.
What is the alternative
1. you can create a REST request with all your data translated as json, send it to other party.
2. use ORM likr hibernation to store the state of your object in db.
Lets come back...
So make object state storable, java told if you want java to store the state of your object then tell the java same by implementing "serializable" interface and java will perform the serialization and deserialization algorithm for your object, if you use
objet.writeObject() and object.readObject().
objet.writeObject() and object.readObject().
what is algorithm (in short) during serialization
upon, obj.writeObject()jvm will
1. store the description of your serializable class, like its name, its serialversionUid,package name, what are fields, number of fields, type of fields
2. Same for its parents and its parents and on.
3. then it will store the the actual value of its top class parents class variable.
4. then come down to child, and child and store its variables actual value.
5. if during variables value assignment, if jvm find that variables is type of some user defined class(which implements serializable or not).
6. then process 1-2 repeats of storing that class definition, like its uid, number of variables, tupe of variables, up its parents hirarchhy, and the assign and store all the actual values.
7. then covert in to byte[] , write to file, db etc.
IMPORTANCE OF SERIALVERSIONUID
The idea of having serialVersionUID, it to force an update in client/de-serialization class and tell him, look, we updated our serializing class, if you want to use our new serialized object, you must update your de-serializing class as well.
How developer does it ?:
While modifying the new serializing class, he also changes the serialVersionUID manully.
Use case-1:
Code serialized the Object with uid = 10L
Client has a class with uid=10, which deserilised it , works fine both object's uid and deserilizing class's uid matches.
Use case-2:
Server has serialized few object on 1st Jan in DB with uid=10 and on 1st Feb server modified the code by adding a new field say "address" and removed old field "name" and changed uid=20 and client after 1 month, still has old class with uid=10.
then client will be able to de-serialized all the object which was stored during 1st Jan, but when it tries to de-serialized new object which were created after 1st Feb(with uid=20).
it will throw InvalidClassException, during de-serialized.
Why ?
Because if client doesn't update its de-serializing class, then it will not be able to recreate, new properties like address and will not find "name" value.
Hence after de-serialized the object will have name=null and adress will not at all be there.
Hence there is inconsistent, of object state and all purpose of saving/trasfering the state of object it lost/not achieved.
No comments:
Post a Comment