transient in java

transient : it's keyword in Java and can be appied only to variable.

If you marked variable as transient means, the variable is not part of the persisten state of an object. i.e Prevents fields from even being serialized. transient fields are always skipped when objects are serialized, telling the JVM to skip(ignore) this variable when you attempt to serialize the object declaring it.

For example : Here's an Image class which holds and image and a thumbnail:

class Image implements Serializable
    private Image image;
    private transient Image thumbnailImage;

    private void generateThumbnail()
        // Generate thumbnail.

    private void readImage(ObjectInputStream inputStream)
            throws IOException, ClassNotFoundException

In this example, the thumbnailImage is a thumbnail image that is generated by invoking the generateThumbnail method.

The thumbnailImage field is marked as transient, so only the original image is serialized rather than persisting both the original image and the thumbnail image. 

* transient variables aren't appropriate for equals() and hashCode().
* transient keyword indicates which variables are not to have their data written to an ObjectStream.

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...