File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes serialization Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "serialization" Watch "serialization" New topic


nirjari patel
Ranch Hand

Joined: Apr 23, 2009
Posts: 386
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.

Christophe Verré

Joined: Nov 24, 2005
Posts: 14688

Why do we need to preserve that object ?

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.

[My Blog]
All roads lead to JavaRanch
Monu Tripathi

Joined: Oct 12, 2008
Posts: 1369

(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.

[List of FAQs] | [Android FAQ] | [Samuh Varta]
nirjari patel
Ranch Hand

Joined: Apr 23, 2009
Posts: 386
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.
Brian Legg
Ranch Hand

Joined: Nov 07, 2008
Posts: 488
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.

~Currently preparing for SCJP6
I agree. Here's the link:
subject: serialization
It's not a secret anymore!