• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

question reg. Serialization process

 
Poornachandran R
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hai,
An article from Sun reg the same can be found here
http://developer.java.sun.com/developer/technicalArticles/Programming/serialization/
I have some doubts:
public class A implements Serializable {
private void writeObject(ObjectOutputStream out) throws IOException
{
out.defaultWriteObject();
}
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException
{
in.defaultReadObject();
}
}
In the above example,
1) Can I give the following line just after line of the method writeObject.
out = new ObjectOutputStream(fos); // assume fos - FileOutputStream.
2) What is the difference between this approach and Externalizable ? Because, in both the approaches we define our own way of how Object is written and read.
Poornachandran
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey, serialization is out of the exam scope.
I will ask for them to move this thread to a more appropiate forum. Please do not add more replies until it is moved.

Can I give the following line just after line of the method writeObject.

do you mean?

out.defaultWriteObject();
out = new ObjectOutputStream(fos); // assume fos - FileOutputStream.

You can but it has no purpouse. The proper sequence would be:

out = new ObjectOutputStream(fos);
aSerializableObject.writeObject(out);


What is the difference between this approach and Externalizable ? Because, in both the approaches we define our own way of how Object is written and read?

In the default serialization process the private instance fields are also serialized. You must be aware of it and and make them transient if this behaviour does not suit you.
If the object to be serialized has a structure of more objects, most of the times you do not want to serialize the structure but only the content of it. Again, the default serialization will process all the state of an instance: both the "logical" and "physical" state an object. --generally you only want to serialize the "logical" state--
Externalizable does not buy anything to you. You must specify exactly what to serialize and deserialize. See the following URL for an example.
Read more about
serialization by Bruce Eckel.
An impressive book Effective Java Programming Language Guide, by Joshua Bloch, also deals with the matter.
 
Frank Carver
Sheriff
Posts: 6920
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moved to Java In General (intermediate)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic