Variables may be marked transient to indicate that they are not part of the persistent state of an object.
Here's a GalleryImage class which contains an image and a thumbnail derived from the image:
class GalleryImage implements Serializable
private Image img;
private transient Image thumbnailImg;
private void generateThumbnail()
// Generate thumbnail.
private void readObject(ObjectInputStream iStream)
throws IOException, ClassNotFoundException
In this example, the thumbnailImg is a thumbnail image that is generated by invoking the generateThumbnail method.
The thumbnailImg field is marked as transient, so only the original image is serialized rather than persisting both the original image and the thumbnail image. This means that less storage would be needed to save the serialized object. (Of course, this may or may not be desirable depending on the requirements of the system -- this is just an example.)
At the time of deserialization, the readObject method is called to perform any operations necessary to restore the state of the object back to the state at which the serialization occurred. Here, the thumbnail needs to be generated, so the readObject method is overridden so that the thumbnail will be generated by calling the generateThumbnail method.