Back

Explore Courses Blog Tutorials Interview Questions
+1 vote
2 views
in Java by (1.1k points)

Why does Java have transient fields?

2 Answers

0 votes
by (13.2k points)
edited by

Before getting to transient we must understand serialization as transient is a variable modifier which is used in serialization.

Serialization

Serialization is often described as a process in which the conversion of state of the object into byte stream takes place, while deserialization is the reverse process. This essentially makes the state of the object persistent. This is important in networking programming very much as the objects to be transmitted have to be converted into bytes.

Transient keyword and purpose


Now when the serialization takes place if we do not want to save value of specific variables then we can use transient. It also plays a vital role in security aspect of the variables, there are many cases when we do not want to save some sort of private data in the files to give them an extra layer of protection. It is considered a good programming practise to use transient with confidential fields of a class during serialization.

0 votes
by (32.3k points)

If you want to define variables that you don't want to serialize.

In an object, you may have information that you don't want to serialize/persist (perhaps a reference to a parent factory object), or perhaps it doesn't make sense to serialize. 

Want to learn Java from basics! Refer to this end to end video on Java:

Marking these as 'transient' means the serialization mechanism will ignore these fields.

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

Browse Categories

...