Serializability of a class is enabled by the class implementing the java.io.Serializable interface. Classes that do not implement this interface will not have any of their state serialized or deserialized. All subtypes of a serializable class are themselves serializable. The serialization interface has no methods or fields and serves only to identify the semantics of being serializable.
http://java.sun.com/docs/books/tutorial/essential/io/objectstreams.html
Quick demo of "implements Serializable" versus not:
import java.io.*;
class S implements Serializable {}
class NotS {}
public class SerializableTest {
public static void main(String[] args) {
test(new S());
test(new NotS());
}
static void test(Object o) {
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream(); //just for a sink
ObjectOutputStream out = new ObjectOutputStream(baos);
out.writeObject(o);
} catch (IOException e) {
e.printStackTrace();
}
}
}
Thank you all for the help.
But one thing. I saw in some application, it has something like this:
public class TestForm implements Serializable {
List testList;
String test;
...
}
But I did not find corresponding OutputStream and InputStream (to serialize and deserialize the object). Any idea why the TestForm (a form bean class) need to implements the Serializable? What if it does not implements the Serializable?
Thanks
Scott