With Transient variable we can stop serializing the required values, but after deserialization we are getting default values of transient variables and we are loosing the original values. So then what is the need of creating transient variable instead we can skip creating the variable itself. If possible how to get the original value of transient variable.

- 305,947
- 44
- 307
- 483

- 111
- 1
- 3
- 10
-
Your question doesn't make sense. The transient keyword is there to prevent Serialization. You use it when you don't want the variable serialized. If you want it serialized, don't make it transient. – user207421 Aug 12 '14 at 22:49
-
I am creating two files, one is for manager and other is for employee. Manager can see all the details like password also so here I am not using any transient value. But while creating a employee file I am adding transient for password. So he can't see the password – Satheesh Guduri Sep 16 '20 at 09:31
4 Answers
The idea of a transient variable is that there's no meaning to persist its original value in serialized form, since it wouldn't be in a valid state after de-serialization (think, for example, about a member variable that holds an open Socket
).
After de-serialization of the object, the transient variable should be initialized by some method call (for example, the getter of that member may initialize it if it's null).

- 387,369
- 54
- 702
- 768
-
Consider, if there are 2 variables A and B both have values before serialization, A is transient here. After deserialization A lost his value but still B has. Then what is the need of serialization wrt A. It is loosing its value while serialization, if required after deserailization also we can create A. Correct me if i'm wrong – ADITYA VALLURU Aug 12 '14 at 18:48
-
@ADITYAVALLURU If you need the value of A to be serialized, don't define it transient. Only if there's no meaning to persisting it (since it contains a value that is valid for a limited time) it should be transient. – Eran Aug 12 '14 at 18:59
As for the explanation of Why is there a transient keyword? / What is the use of transient variables? I like to point to this question: Why does Java have transient fields?
It neatly explains that transient variables are used for i.a. for performance reason, say pre-calculating certain values that come from the combination of other values stored in the object. You need them in your code, but they would only take up valuable space when the object is serialized and sent/stored somewhere.
Another use would be, as mentioned by Eran, to store variables in the object that are necessary for it's function but at the same time are for example dependent on the underlying system.
As for How to get the original value of the transient variable?, there is no clean way to do that afaik. Especially if you serialize and object for transfer between two applications there is no way as the data simply does not exist.
Consider very basic scenario Person
class
class Person{
private Name
private DOB
private Age}
In this, storing the Name and DOB makes sense, but storing Age
doesn't as it always change on a daily basis, so declare it as transient
and it can be always calculated as Current date - DOB
, which will give the accurate Age.

- 575
- 1
- 8
- 24
-
yup that will be gone, since there is no sense to store that variable... so u need to recalculate it if/whenever you need it. – deejay Aug 12 '14 at 18:57
-
Then what is the use of transient variable, assigning the default values to the variables or not allowing variables to serialization ?if first one is correct we can do that manually also, java no need to create a keyword for that right – ADITYA VALLURU Aug 12 '14 at 19:09
-
-
@Aditya we need to mark a field as `transient` when that field is dependent on other fields. Like in this example, the field `Age` is being calculated by `Current date - DOB`. So it makes no sense persisting a dependent fields. – Ram Sep 03 '18 at 15:17
First of all transient
keyword is used along with instance variables to exclude them from serialization process. if a field is transient its value will not be persisted.
The perfect use of Transient variable can be seen in Hibernate.
For and example in you Database table there are only two column (name and surname)
but in your POJO entity you are having three variable (name , surname and age)
in this situation you can make Transient to age field and you can save your entity without any complain as age variable becomes as transient not going to persist.

- 5,061
- 3
- 26
- 39