Transient in Java is used to mark the member variable not to be serialized when it is persisted to streams of bytes. This keyword plays an important role to meet security constraints in Java. It ignores the original value of a variable and saves the default value of that variable data type.
The transient keyword in Java is used to avoid serialization. If any object of a data structure is defined as a transient , then it will not be serialized. Serialization is the process of converting an object into a byte stream.
A field which is declare with transient modifier it will not take part in serialized process. When an object is serialized(saved in any state), the values of its transient fields are ignored in the serial representation, while the field other than transient fields will take part in serialization process.
Volatile keyword is used to flush changes directly to the main memory. The transient keyword is used to exclude variable during serialization.
Google is your friend - first hit - also you might first have a look at what serialization is.
It marks a member variable not to be serialized when it is persisted to streams of bytes. When an object is transferred through the network, the object needs to be 'serialized'. Serialization converts the object state to serial bytes. Those bytes are sent over the network and the object is recreated from those bytes. Member variables marked by the java transient keyword are not transferred, they are lost intentionally.
Example from there, slightly modified (thanks @pgras):
public class Foo implements Serializable
{
private String saveMe;
private transient String dontSaveMe;
private transient String password;
//...
}
Transient variables in Java are never serialized.
It means that trackDAO
should not be serialized.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With