Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Where should we implement readObject() and writeObject() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Where should we implement readObject() and writeObject()" Watch "Where should we implement readObject() and writeObject()" New topic

Where should we implement readObject() and writeObject()

Thomas Bertran

Joined: Sep 19, 2007
Posts: 1
Question 15 of chapter 6 in K&B reads that "you would implement the readObject() method in SpecialSerial".
I am trying to understand why implement it in SpecialSerial as opposed to TestSer, where the serialization/deserialization is done.

I didn't find any explanation in the book.
Thanks for your help.
marc weber

Joined: Aug 31, 2004
Posts: 11343

Welcome to JavaRanch!

This is a good question. See pages 449-452 for an explanation.

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
Kelvin Chenhao Lim
Ranch Hand

Joined: Oct 20, 2007
Posts: 513
Most likely, you're confused because there are actually two different "readObject()" methods being used here. One is the "Object readObject()" method of ObjectInputStream, which is what TestSer is invoking. However, you can also implement a different readObject() method in the Serializable class itself to customize the serialization process. This other readObject() method has a different signature from the one invoked by TestSer: "void readObject(ObjectInputStream in)". Same thing with writeObject().

I always thought it was a little unfortunate that Java's designers decided to use the same name for those two methods. Something like "readObject()" and "readThisObject()" would have been a bit better, in my opinion.

SCJP 5.0
It is sorta covered in the JavaRanch Style Guide.
subject: Where should we implement readObject() and writeObject()
Similar Threads
Regarding Serialization
Question about Serialization
Static values don't get serialized, yet the code seems to show the contrary
serialization question - pls help me!
Why static variable serialized here?