The simplest thing you need to do is add a private static field to your class with the name serialVersionUID. For instance:
private static final long serialVersionUID = 1L;
This is used by the default serialization mechanism to match up class names and formats.
You can then write instances of your object to an ObjectOutputStream and read them back from an ObjectInputStream:
Region r = . . .;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bos);
oos.writeObject(r);
oos.close();
ObjectInputStream ois = new ObjectInputStream(
new ByteArrayInputStream(bos.getBytes()));
Region r2 = (Region) ois.readObject();
// voilà - a very expensive clone()!
For more control over your object serialization, you can implement these methods:
private void writeObject(java.io.ObjectOutputStream out)
throws IOException
private void readObject(java.io.ObjectInputStream in)
throws IOException, ClassNotFoundException;
private void readObjectNoData()
throws ObjectStreamException;
You then have total control over the serialization of your objects. For more info, see the docs on Serializable.
UPDATE: Strictly speaking, you don't need to declare serialVersionUID; the runtime environment will calculate one for you automatically if it's missing. However, the docs have this to say about it (emphasis in the original):
However, it is strongly recommended that all serializable classes explicitly declare serialVersionUID values, since the default serialVersionUID computation is highly sensitive to class details that may vary depending on compiler implementations, and can thus result in unexpected InvalidClassExceptions during deserialization.