• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

question reg. Serialization process

 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 7001
6
Eclipse IDE Python C++ Debian Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Moved to Java In General (intermediate)
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic