Why java have transient fields

0 votes
May 28, 2018 in Java by Akrati
• 3,190 points
948 views

1 answer to this question.

0 votes

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
    {
        iStream.defaultReadObject();
        generateThumbnail();
    }    
}

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.

answered May 28, 2018 by Daisy
• 8,140 points

Related Questions In Java

0 votes
0 answers

why so JavaScript and java have similar name

Nov 29, 2023 in Java by Priyanka
• 700 points
660 views
0 votes
1 answer

Why the main() method in Java is always static?

As you might know, static here is ...READ MORE

answered May 9, 2018 in Java by geek.erkami
• 2,680 points
2,058 views
0 votes
1 answer

What is the concept of Immutability for strings in Java ? Why are strings immutable ?

According to Effective Java, chapter 4, page 73, ...READ MORE

answered May 11, 2018 in Java by Rishabh
• 3,620 points
1,548 views
0 votes
1 answer

Does python have an equivalent to Java Class.forName()?

This is found in the python standard ...READ MORE

answered Jun 1, 2018 in Java by Parth
• 4,640 points
1,394 views
+5 votes
4 answers

How to execute a python file with few arguments in java?

You can use Java Runtime.exec() to run python script, ...READ MORE

answered Mar 27, 2018 in Java by DragonLord999
• 8,450 points

edited Nov 7, 2018 by Omkar 80,867 views
+1 vote
1 answer

How to handle drop downs using Selenium WebDriver in Java

First, find an XPath which will return ...READ MORE

answered Mar 27, 2018 in Selenium by nsv999
• 5,500 points
8,233 views
0 votes
1 answer

What are the differences between getText() and getAttribute() functions in Selenium WebDriver?

See, both are used to retrieve something ...READ MORE

answered Apr 5, 2018 in Selenium by nsv999
• 5,500 points
17,327 views
0 votes
1 answer

Selenium JARS(Java) missing from downloadable link

Nothing to worry about here. In the ...READ MORE

answered Apr 5, 2018 in Selenium by nsv999
• 5,500 points

edited Aug 4, 2023 by Khan Sarfaraz 4,744 views
0 votes
2 answers

Why are you not able to declare a class as static in Java?

A static keyword  can be used with ...READ MORE

answered Jun 11, 2019 in Java by Neha
• 330 points
1,415 views
0 votes
1 answer

Why doesn't RecyclerView have onItemClickListener()?

I like this way and I'm using ...READ MORE

answered Dec 21, 2018 in Java by Daisy
• 8,140 points
880 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP