group-icon
Java Developers
Group for Java developers and programmers. Ask questions and get answers related to Java programming
263 Members
Join this group to post and comment.
Archana Priyadarshini
Archana Priyadarshini
Computer Science
28 Jan 2019

Transient variable and volatile variable in java

Transient

In Java, it is used to specify the variable is not being serialized. Serialization is a process of saving an object’s state in Java. When we want to persist and the object’s state by default, all instance variables in the object are stored. In some cases, if we want to avoid persisting few variables because we don’t have the necessity to transfer across the network. So, declare those variables as transient.

If the variable is confirmed as transient, then it will not be persisted. Transient keyword is used with that instance variable which will not participate in the serialization process. We cannot use static with a transient variable as they are part of the instance variable.

Volatile

Volatile keyword is used with only variable in Java and it guarantees that the value of the volatile variable will always be read from main memory and not from Thread’s local cache, it can be static.