- java defines two special type of modifiers
transient
andvolatile
. - if an instance variable is declared
transient
, its value will not be persisted when the object is stored.
class Main{
transient int a; // not persist on saving object of Main
int b; // persist on save
}
- In multithreading multiple threads share the same variable. threads will keep a private copy of such variable,and the master copy is updated at various times.
To make sure that the master copy of the variable always reflects its current state, we can specify the variable asvolatile
. - This tells the compiler that it must always use the master copy of a volatile variable or at least always keeps any private copies up-to-date with the master copy and vice versa.