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

    Default Serialization -- Use of defaultwriteobject()


    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?


  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Voorschoten, the Netherlands
    Blog Entries
    Rep Power


    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,

    Build a wall around Donald Trump; I'll pay for it.

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


    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, 05:45 AM
  2. serialization
    By elsa14290 in forum Advanced Java
    Replies: 1
    Last Post: 02-06-2010, 05:03 PM
  3. Serialization
    By thayalan in forum Advanced Java
    Replies: 4
    Last Post: 08-03-2009, 11:22 PM
  4. Serialization
    By vijay24805 in forum Threads and Synchronization
    Replies: 1
    Last Post: 04-10-2009, 10:16 PM
  5. Replies: 0
    Last Post: 04-04-2008, 03: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