Persisting state of objects

11 December 2007

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

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

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.

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.

del.icio.us:Persisting state of objects  digg:Persisting state of objects  spurl:Persisting state of objects  wists:Persisting state of objects  simpy:Persisting state of objects  newsvine:Persisting state of objects  blinklist:Persisting state of objects  furl:Persisting state of objects  reddit:Persisting state of objects  fark:Persisting state of objects  blogmarks:Persisting state of objects  Y!:Persisting state of objects  smarking:Persisting state of objects  magnolia:Persisting state of objects  segnalo:Persisting state of objects  gifttagging:Persisting state of objects

Top Of Page | Trackback

If you found this page useful, consider linking to it. Simply copy and paste the code below into your web site.

It will look like this: Persisting state of objects

Leave a Reply