View RSS Feed

My Java Tips

Persisting state of objects

Rate this Entry
by , 11-13-2011 at 12:24 PM (682 Views)
Serializable interface has no methods and fields so a class that implementing this interface does not have to override any method. The state of objects of serializable class, can be saved and restored later. The state is saved on a container that may be transient (RAM-based) or persistent (disk-based).


For network communication, the objects that you want to transfer through streams, should be Serializable. For instance, Vector, ArrayList etc implement Serializable interface so their state can be saved on the streams or on disk.

Sometimes, you want the objects of your own class to be Serialized. For that, you have to implement Serializable interface. Time for an example:

MyClass.java

Java Code:
import java.io.Serializable;

public class MyClass implements Serializable{

	private int taskId;
	private String taskName;
	public MyClass()
	{

	}
	public MyClass(int taskId, String taskName) {
		this.taskId = taskId;
		this.taskName = taskName;
	}
	public int getTaskId() {
		return taskId;
	}
	public void setTaskId(int taskId) {
		this.taskId = taskId;
	}
	public String getTaskName() {
		return taskName;
	}
	public void setTaskName(String taskName) {
		this.taskName = taskName;
	}
}
MainClass.java

Java Code:
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

public class MainClass {

	public static void main(String[] args) throws Exception {

		MyClass obj1 = new MyClass(1,"MissionJAva");

		FileOutputStream f_out = new FileOutputStream("C:\myclass_obj.data");
		ObjectOutputStream obj_out = new ObjectOutputStream (f_out);
		obj_out.writeObject (obj1);
		obj_out.flush();

		FileInputStream f_in = new FileInputStream("C:\myclass_obj.data");
		ObjectInputStream obj_in = new ObjectInputStream (f_in);

		MyClass obj_read = new MyClass();
		obj_read = (MyClass)obj_in.readObject();

		System.out.println("Task id: " + obj_read.getTaskId());
		System.out.println("Task name: " + obj_read.getTaskName());
	}

}
Output:


Task id: 1
Task name: MissionJAva

MyClass implements Serializable interface so the state of its objects can be persisted as seen in the above example. I tried the example above with following MyClass signature.

Java Code:
public class MyClass{

}
When I tried to compile the classes, I got following error in MainClass.


Exception in thread "main" java.io.NotSerializableException: MyClass
at java.io.ObjectOutputStream.writeObject0(Unknown Source)
at java.io.ObjectOutputStream.writeObject(Unknown Source)
at MainClass.main(MainClass.java:16)

Conclusive note is that implement Serializable interface if you want to save the state of the object on disk or for writing to/reading from streams.

Submit "Persisting state of objects" to Facebook Submit "Persisting state of objects" to Digg Submit "Persisting state of objects" to del.icio.us Submit "Persisting state of objects" to StumbleUpon Submit "Persisting state of objects" to Google

Comments