File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Serialization - overriding readObject() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Serialization - overriding readObject()" Watch "Serialization - overriding readObject()" New topic

Serialization - overriding readObject()

Kiran Gavate

Joined: Jan 18, 2007
Posts: 25
Hello ranchers,

I am trying to understand the overriding of writeObject() and readObject() to manually serialize non-serializable members of a class.

I am writing the class instance into a file in one java program and then reading it using a different java program. I have overridden the writeObject() and readObject(). Overridden writeObject() works fine, but when I try to read the saved object, the overridden readObject() is not called at all. I am unable to see why it is not working. Could you please identify where is the problem in the following code?

1. - serializes the object. I am overriding wireObject() to manually serialize a static variable in the class.

2. - de-serialize the object. The problem is here - overridden readObject() is not being called at all.

I can not see the output "Reading static variable manually...", it means it is not going to the overridden readobject() method.

Henry Wong

Joined: Sep 28, 2004
Posts: 20535

I can not see the output "Reading static variable manually...", it means it is not going to the overridden readobject() method.

To deserialize a SerTest object, it will use the readObject() method, if available, of the SerTest class. It will use the readObject() method, of the ReadSerTest class, when deserializing a ReadSerTest object.


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Kiran Gavate

Joined: Jan 18, 2007
Posts: 25
Moved the readObject() method to Worked like a charm!

Thank you so much.

From the example in the k&b book it was not clear to me that both the readObject() and writeObject() overrides should be in the same class (it looks so obvious now as I am writing it!)

Here is the complete listing if somebody wants to try it out.



harsha balluru

Joined: Feb 06, 2009
Posts: 14
I correct something here:
By defining the methods readObject() and writeObject(), you actually "implement" the methods; You can NOT "override" them. These methods you are implementing here come from the "Serializable" interface implemented by the class whose object's state you are trying to save.
In fact the classes and have declared the methods writeObject() and readObject() as final.
Kushan Athukorala
Ranch Hand

Joined: Aug 09, 2010
Posts: 33
Hi Kiran,

I am adding following to harsha's comments.

Since no methods are defined in Serializable interface, such interfaces are called Marker Interfaces.

Remember this in the exam. Serializable and RandomAccess are marker interfaces


Kushan Athukorala
OCPJP 6.0 [86%]
I agree. Here's the link:
subject: Serialization - overriding readObject()
It's not a secret anymore!