Results 1 to 3 of 3
- 03-10-2011, 08:40 AM #1
Member
- Join Date
- Mar 2011
- Posts
- 16
- Rep Power
- 0
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
- 03-10-2011, 10:40 AM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,394
- Blog Entries
- 7
- Rep Power
- 17
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 03-16-2011, 12:09 PM #3
Member
- Join Date
- Mar 2011
- Posts
- 16
- Rep Power
- 0
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
-
Serialization
By twiggy62 in forum New To JavaReplies: 2Last Post: 02-12-2010, 04:45 AM -
serialization
By elsa14290 in forum Advanced JavaReplies: 1Last Post: 02-06-2010, 04:03 PM -
Serialization
By thayalan in forum Advanced JavaReplies: 4Last Post: 08-03-2009, 10:22 PM -
Serialization
By vijay24805 in forum Threads and SynchronizationReplies: 1Last Post: 04-10-2009, 09:16 PM -
What is Serialization and de-serialization in Java
By Java Tip in forum java.ioReplies: 0Last Post: 04-04-2008, 02:47 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks