Posted by: Naveen Kapoor on: December 29, 2008
Java allows us to create reusable objects in memory. All of those objects exist only as long as the Java virtual machine. With object serialization, we can flatten our objects and reuse them in powerful ways.
Object serialization is the process of saving an object’s state to a sequence of bytes, and also rebuilding those bytes into a live object at some future time.
There are three different ways to perform serialization -
1) Using the default protocol,
2) Customizing the default protocol, and
3) Creating your own protocol
1) Lets learn the first one first (i.e.) Default Protocol:
To make an object as a persistent object, an object is marked serializable by implementing the java.io.Serializable interface which signifies to the existing API’s that the object can be flatten into bytes abd vice-versa.
Example :
import java.io.Serializable;
public class PersistentExample implements Serializable
{
private String persist_String;
public PersistentExample()
{
persist_String= “Naveen Kapoor”;
}
public String getPersistString()
{
return persist_String;
}
}
Next step is to actually persisting an object and this we can achieve with java.io.ObjectOutputStream class. This class is a filter stream and this is wrapped around a low level byte stream known as node stream which take care of serialization protocol for us.
This node stream is capable of writing to a file system or across the sockets.
Example :
import java.io.ObjectOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class CustomizeProtocol
{
public static void main(String [] args)
{
String filename = “custiomFile.txt”;
if(args.length > 0)
{
filename = args[0];
}
PersistentExample Persist_Example = new PersistentExample ();
FileOutputStream fos = null;
ObjectOutputStream out = null;
try
{
fos = new FileOutputStream(filename);
out = new ObjectOutputStream(fos);
out.writeObject(Persist_Example); //
out.close();
}
catch(IOException ex)
{
ex.printStackTrace();
}
}
}
above code : out.writeObject(Persist_Example) will trigger the serialization mechanism and will flatten the object to the file.
Not all objects in java are persistent, Only objects that implements serializable interfaces are persistent. Certain system level classes such as thread, outputStream and its subclasses are not serializable. And it would not make any sense if they were. for example, Thread running in my JVM would be using my system’s memory. Persisting it and trying to run it in your JVM would make no sense at all.
If we have a class that contains an instance of a thread, Is it possible to persist that class.
Yes… As long as we tell serialization process by marking our class’s thread object as Transient.
The bottom line: you must mark transient any field that either cannot be serialized or any field you do not want serialized.
************************************************************
Your comments and corrections are most welcome
December 29, 2008 at 5:19 pm
Nice one