Results 1 to 3 of 3
  1. #1
    garyiskidding is offline Member
    Join Date
    Mar 2011
    Posts
    21
    Rep Power
    0

    Default Serialization -- Use of defaultwriteobject()

    Hello,

    while we use the readObject() and writeObject() methods during Serialization, what kind of role does the defaultReadObject/defaultwriteobject() methods play there?

    In one of the older posts in this forum, i read that defaultReadObject/defaultwriteobject() invoke the "default" serialization/deserialization for that object.

    Here are the questions that i have :

    1) If defaultwriteobject() invokes the default serialization behavior, how is it different from the case when we do not use the readObject/writeObject methods, and simply extend serializable interface. Is'nt that default serialization?

    2) When should defaultReadObject/defaultwriteobject() method be used compulsorily, with the readObject/writeObject methods?

    3) When should the readObject/writeObject be used without the defaultReadObject/defaultwriteobject() methods?

    -Thanks
    Gary

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,371
    Blog Entries
    7
    Rep Power
    20

    Default

    Note that defaultWriteObject() may only be called by the writeObject() method (it has no parameters, it takes them from the ObjectOutputStream object). Subclasses can override this method or, if the ObjectOutputStream default constructor was used to create the object, the protected writeObjectOverride() method (which is empty in the base class object) is called and therefor needs to be overridden.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    garyiskidding is offline Member
    Join Date
    Mar 2011
    Posts
    21
    Rep Power
    0

    Default

    I got the answer that i was looking for. Here it is:

    Answer 1) defaultwriteobject() is identical to default Serialization without writeObject()

    Answer 2) When there is only a little tweak in the standard Java serialization process that you want to do manually, for everthing else, you need the default process to kick in. In this case, the call to defaultWriteObject() is the first thing in the program.

    Answer 3) When you simply do not want Java to come in and help during serialization. The entire serialization would now be done manually. I think this case comes closest to the "Externalizable" functionality.

Similar Threads

  1. Serialization
    By twiggy62 in forum New To Java
    Replies: 2
    Last Post: 02-12-2010, 04:45 AM
  2. serialization
    By elsa14290 in forum Advanced Java
    Replies: 1
    Last Post: 02-06-2010, 04:03 PM
  3. Serialization
    By thayalan in forum Advanced Java
    Replies: 4
    Last Post: 08-03-2009, 10:22 PM
  4. Serialization
    By vijay24805 in forum Threads and Synchronization
    Replies: 1
    Last Post: 04-10-2009, 09:16 PM
  5. Replies: 0
    Last Post: 04-04-2008, 02:47 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •