The Design of Software (CLOSED)

A public forum for discussing the design of software, from the user interface to the code architecture. Now closed.

The "Design of Software" discussion group has been merged with the main Joel on Software discussion group.

The archives will remain online indefinitely.

Java Serialization?

Ok, I was thinking, how does java un-serialize an object, and set all its private variables?  Does anyone know how this is done? Without dynamic code generation, I can't figure out how.  If anyone knows, or has a good guess, feel free to share.  Thanks.
Monday, November 15, 2004
Just don't implement serialized interface?
Carfield Yim Send private email
Monday, November 15, 2004
Yeah...that would stop it from being would marking it transient...but i'm just curious how it actually does it.
Monday, November 15, 2004
Why don't you take a look at the source code and see?
John Topley Send private email
Tuesday, November 16, 2004
Rhys Keepence Send private email
Tuesday, November 16, 2004
Probably with reflection. Damn nice stuff, reflection is, and allows access to private variables.

java.lang.reflect. If you haven't yet been exposed to this package, I recommend you do so. It is pure magic.
Herr Herr Send private email
Tuesday, November 16, 2004
I'm familiar with java.lang.reflect, but not as well as originally thought.  Apparently theres a setAccessible method that I missed.
Tuesday, November 16, 2004
As far as I know, private modifiers don't apply during deserialization. It is even explicitely mentioned in the SUN documentation on the issue and it is the reason why you must make object Serializable explicitly. Basically, implementing the Serializable interface means saying to the JVM: "The private members of the class are accessible for serialization purposes.".

BTW, data members access is not the most important issue. Imagine that you have two versions of the serializaed class on two platforms...
Thursday, November 18, 2004
Make your class "implements Serializable"

 private void writeObject( out)
    throws IOException
 private void readObject( in)
    throws IOException, ClassNotFoundException;

The writeObject method is responsible for writing the state of the object for its particular class so that the corresponding readObject method can restore it. The default mechanism for saving the Object's fields can be invoked by calling out.defaultWriteObject. The method does not need to concern itself with the state belonging to its superclasses or subclasses. State is saved by writing the individual fields to the ObjectOutputStream using the writeObject method or by using the methods for primitive data types supported by DataOutput.
Friday, November 26, 2004

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics
Powered by FogBugz