41.3k views
5 votes
Explain the usage of the keywordtransient?

1 Answer

4 votes

Answer:

'transient' is a java language keyword used to mark fields which should not be included in object serialization.

Step-by-step explanation:

When an object is serialized, that is, when the object is persisted in the file system, some of the fields may not be relevant for storage and restoration. An example might be a connection object to a database which might become stale by the time when the object is deserialized. Such fields can be marked as 'transient' to ensure that they are not part of the serialization mechanism for the object.Usage is as follows:

transient <datatype> <variable-name>;

e.g.,

class Demo{

String name;

int age;

transient int income;

}

User Deesarus
by
5.2k points