Net-informations.com
SiteMap  | About    

What is transient keyword in Java?

What is serialization?

Serialization in java is a mechanism of writing the state of an object into a byte stream and deserialization is the process of converting a stream of bytes back into a copy of the original object. More about... Java Serialization

transient keyword

The transient keyword in Java is used to indicate that a field should not be serialized. In Java , by default, all of object's variables get converted into a persistent state . In rare 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 . That means, when JVM comes across transient keyword, it ignores original value of the variable and save default value of that variable data type. That is the main purpose of the transient keyword. It is good habit to use transient keyword with private confidential fields of a class during serialization. Probably the best example is a Thread field. There's normally no reason to serialize a Thread, as its state is very 'flow specific'.

Example

Serializing an Object

Deserialization of Object

When you try to deserialize, you wont get back the transient field "age" because its not serialized.









net-informations.com (C) 2017    Founded by raps mk
All Rights Reserved. All other trademarks are property of their respective owners.