The
Before understanding the
Now what is the
transient
keyword in Java is used to indicate that a field should not be part of the serialization (which means saved, like to a file) process.Before understanding the
transient
keyword, one has to understand the concept of serialization.What is serialization?
Serialization is the process of making the object's state persistent. That means the state of the object is converted into a stream of bytes to be used for persisting (e.g. storing bytes in a file) or transferring (e.g. sending bytes across a network). In the same way, we can use the deserialization to bring back the object's state from bytes. This is one of the important concepts in Java programming because serialization is mostly used in networking programming. The objects that need to be transmitted through the network have to be converted into bytes. For that purpose, every class or interface must implement the
Serializable
interface. It is a marker interface without any methods.
Now what is the transient
keyword and its purpose?
By default, all of object's variables get converted into a persistent state. In some cases, you may want to avoid persisting some variables because you don't have the need to persist those variables. So you can declare those variables as
transient
. If the variable is declared as transient
, then it will not be persisted. That is the main purpose of the transient
keyword.
Comments
Post a Comment