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


Harry Tuttle

Joined: Apr 08, 2010
Posts: 24

I'm just reading the serialization in the SCJP-Book 6.0 and how to use writeObject and readObject if an object is not serializable but used in a composition at page 465. suppose the not serializable object has a juge of member variables. if i got the idea in the book i have to write them all manually and read them one by one and assign them to the not serializable object. is that right?
Sebastian Janisch
Ranch Hand

Joined: Feb 23, 2009
Posts: 1183
No ...

you have a defaultWriteObject and defaultReadObject method which you want to use if you need to serialize the state of transient variables (if any library you use has classes that don't implement serializable) or if you changed your class and need to do adaptions to the read process of the objects.

JDBCSupport - An easy to use, light-weight JDBC framework -
Rob Spoor

Joined: Oct 27, 2005
Posts: 20276

Sebastian, I think Harry is right. defaultReadObject and defaultWriteObject are used to read/write the non-transient fields. In this case, the non-serializable fields should be made transient. writeObject must then write their fields one by one manually, and readObject must recreate the non-serializable fields and then read their fields one by one manually (or vice versa if you need the fields in constructors). This does mean that these non-serializable fields cannot be final.

You may be able to automate things by using reflection, but be aware that this may (will?) cause problems if you add, remove or change fields.

How To Ask Questions How To Answer Questions
I agree. Here's the link:
subject: Serialization
It's not a secret anymore!