I read that serialization preserves the state of an object. What does this actually mean ? When a program is running and object is created, that object disappears when program is finished. Why do we need to preserve that object ? Is preserving an object is like saving a file ? Incomplete file can be saved and completed later on. Does serialization do something like that with object ?(something of this meaning I read in one article) ? In that case why would an object be incomplete ? This may be simpler concept, but since I am not getting the basic idea clear, I am confused. Please explain this with elaboration.
Imagine a web application, and a server crashes. If an object was serialized (converted into bytes and saved in a file), it can be brought back to its original state after restart. Serialization is also heavily used in RMI, where objects have to be sent among different computers in a network. An object will be serialized (a.k.a marshalling) at one side, sent and deserialized(unmarshalling ) on the other side.
(Read K&B Head First Java..)
Imagine you are playing a computer game and want to save your progress. What do you think happens when you save your game's state? The variables that hold and define the state of your game are written to a storage medium so that it can be retrieved later when you resume.
In Java, state of the application is defined by the objects and the values contained in their fields. Serialization is a mechanism to persist the state of an object to a file.
So when an object is saved in incomplete form, that object is serialized and saved to a file. What file would it be saved to ? Will it be saved on the server ?
As Christopher explained, what if the server crashes ? In that situation, will the objects be serialized automatically ?
When I a program is running, and I have to interrupt that program will it be serialized automatically ? Do I need to put anything in program which will serialize the objects upon save or sudden exit of program ?
I got clear with basic idea now, but still confused with questions mentioned above.
nirjari patel wrote:As Christopher explained, what if the server crashes ? In that situation, will the objects be serialized automatically?
I don't see how that would be possible. If you unplug your pc out of knowhere how could your software suddenly save your status? Serializing is done manually by the programmer when it is appropriate. For instance you would serialize or save the state of a game when the user clicks on "save game". For other applications a timer thread may be running to automatically save the state of the program in intervals to try and prevent major data loss by writing to a temp file and waiting for the user to actually initiate a save.
When you see the word serialize just replace it with save and replace objects with the word data if it makes more sense that way. Also, I don't know what you mean by incomplete objects, sorry.