This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
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.