• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

serialization

 
Ranch Hand
Posts: 386
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Rancher
Posts: 1369
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
(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.
 
nirjari patel
Ranch Hand
Posts: 386
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks.
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.
 
Ranch Hand
Posts: 488
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic