File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes question reg. Serialization process Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "question reg. Serialization process" Watch "question reg. Serialization process" New topic
Author

question reg. Serialization process

Poornachandran R
Ranch Hand

Joined: Sep 11, 2002
Posts: 47
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

Joined: Jul 03, 2001
Posts: 2120
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.


SCJP2. Please Indent your code using UBB Code
Frank Carver
Sheriff

Joined: Jan 07, 1999
Posts: 6920
Moved to Java In General (intermediate)


Read about me at frankcarver.me ~ Raspberry Alpha Omega ~ Frank's Punchbarrel Blog
 
jQuery in Action, 2nd edition
 
subject: question reg. Serialization process
 
Similar Threads
need object persistence using file in Servlet
question reg. Serialization process
What is object serialization?
writeObject & readObject