guys i am going through the head first java i hav gotten that serialized objects save the values of the instance variables..but its written after that "so that an identical instance(object) can be brought back to the heap" i am not able to understand this.....please help!!!
Object is created in RAM which is a volatile memory so in case when your programs stops (or assume computer is switched off) then object will be vanished.
Think, what could be the way you can get the same object back when your computer was running? One way is that you can store the object on Hard Disc when your program is stopping and when you start it again it reads the object from Hard Disc. This way you can get the same object.
Serialization does exactly this thing for you.
Think of this scenario,
1. You have an object where you would want to save list of things you have already completed from your TODO list.
Say here is your TODO list - Calling your aunt. - doing shopping - Mailing important doc - Getting car Fuel
2. Now if your program goes down after you have completed first two items in the list then you may not want to do them again when next time program comes up. So you can Serialize your Already Completed object list and save it for future processing.
3. Next time when your program runs it will read the object from Hard Disc and start processing forward.
Hope this clarifies....
TODO list may not be the best example here.... can you think of some other better scenario?
The JVM can do lots of things you as a programmer can't. That's because the JVM has access to all of its own internals. My guess is that, if it wanted to, the JVM could even change the class of an object.
But serialisation does not recreate the same object; it creates a different object with the same (instance, non-transient) field values.
sir you said that a new object is created but hw can we create a new object when the constructor is not even called???
My guess is that JVM need not worry about calling constructor for a de-serialized object as the object is stored as byte sequence, and JVM must internally know how to just convert this to a heap object.
In my opinion de-serialized bytes should be directly usable bytes in heap as object.