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 Object serialization and order Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Object serialization and order" Watch "Object serialization and order" New topic

Object serialization and order

leo donahue
Ranch Hand

Joined: Apr 17, 2003
Posts: 327
I was just reading in HFJ about object serialization. When we serialize an ojbect with an instance variable that is an object ref, HFJ says that the entire object graph will get serialized. But my question is: "In what order are instance variables serialized"? Does it follow the order in which they appear in the serializing class?

Thanks, leo
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1873
Hi leo
Why would that order matter to us? As far as we get the same object that we wrote its fine I guess.
Tim West
Ranch Hand

Joined: Mar 15, 2004
Posts: 539
It's an interesting academic least, I think so. It was posted somewhere here a while back and got no answer...I can't find anything on Google and I have a few spare minutes, so let's find out:

(See bottom of post for code to HexOutputStream.)
OK, so the output isn't going to be exactly legible, but:

Despite my dodgy HexOutputStream, things seem to work - the "magic number" that starts all serialisation stream (AC ED) is present at the start of the output and the serialisation format version number (00 05) is the current one for my JRE.
OK, so assuming it all works the important things are 61, 62, 63 (chars 'a', 'b', 'c'). In each case, it seems they come out in that order 'a', 'b', 'c'.
Conclusion: variables of the same type are serialised in alphabetical order, regardless of their scope.
Question: do all variables behave like this, regardless of their type? I'd check that out, but it'd mean decoding the serialisation hex dump pretty thoroughly because other types aren't nearly as easy to find as Strings. (Maybe longs containing Long.MIN_VALUE would be easy to find?)
This is hardly scientific, but it's a start. Any advances?

For reference, the HexOutputStream in its dodgy glory:

[ April 27, 2004: Message edited by: Tim West ]
[ April 28, 2004: Message edited by: Tim West ]
I agree. Here's the link:
subject: Object serialization and order
It's not a secret anymore!