Meaningless Drivel is fun!*
The moose likes Beginning Java and the fly likes serialized Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "serialized" Watch "serialized" New topic
Author

serialized

geet kaur
Ranch Hand

Joined: Sep 03, 2008
Posts: 78
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!!!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38513
    
  23
There are two main uses for serialisation:
  • To put the values of (instance only) fields into a file so an identical object can be reconstructed.
  • To divide the object into a series (hence the name) of bytes which can be sent across a network.
  • More details in the Java Tutorials, which you would do well to bookmark.
    Sachin Joshi
    Ranch Hand

    Joined: Aug 06, 2008
    Posts: 83

    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?


    Web Development Tips and Tutorials - By Sachin
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 38513
        
      23
    I can't see anything wrong with that example.

    But serialisation does not recreate the same object; it creates a different object with the same (instance, non-transient) field values.
    geet kaur
    Ranch Hand

    Joined: Sep 03, 2008
    Posts: 78
    sir you said that a new object is created but hw can we create a new object when the constructor is not even called???
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 38513
        
      23
    Can't remember offhand how the new object is created; that is the JVM's problem.

    There are bound to be lots of links about it.
    Anybody else??
    Rob Spoor
    Sheriff

    Joined: Oct 27, 2005
    Posts: 19684
        
      20

    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.


    SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
    How To Ask Questions How To Answer Questions
    Sachin Joshi
    Ranch Hand

    Joined: Aug 06, 2008
    Posts: 83

    I can't see anything wrong with that example.

    But serialisation does not recreate the same object; it creates a different object with the same (instance, non-transient) field values.


    Agree.



    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.

    Experts please correct me if I am wrong here....
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: serialized