Logo 
Search:

Java Articles

Submit Article
Home » Articles » Java » FundamentalRSS Feeds

Transient Variable

Posted By: Ararinda Schmidt     Category: Java     Views: 6577

This article explains about transient variable in java with example.

A transient variable is a variable that may not be serialized. You use the transient keyword to indicate to the Java virtual machine that the indicated variable is not part of the persistent state of the object. Variables that are part of the persistent state of an object must be saved when the object is archived.  

Syntax of Transient Variable

transient type varName = value; 

type is any data type in java.
varName is any valid identifier in java. It is a variable name.
value is an optional. You can initialize variable by assigning its value. 

Example of Transient Variable

Example 1 : Program that illustrates transient variable use in java

class Employee
class GalleryImage
{
    private Image image;
    private transient Image thumbnailImage;

    private void generateThumbnail()
    {
        // Generate thumbnail.
    }

    private void readObject(ObjectInputStream inputStream) throws IOException, ClassNotFoundException
    {
        inputStream.defaultReadObject();
        generateThumbnail();
    }    
}


In above example thumbnailImage is a thumbnail image that is generated by calling 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. This means that less storage would be needed to save the serialized object. 

  
Share: 

 
 
 

Didn't find what you were looking for? Find more on Transient Variable Or get search suggestion and latest updates.

Ararinda Schmidt
Ararinda Schmidt author of Transient Variable is from Frankfurt, Germany.
 
View All Articles

 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
No Comment Found, Be the First to post comment!