Skip to content

Latest commit

 

History

History
17 lines (14 loc) · 840 Bytes

07. Transient and Volatile modifiers.md

File metadata and controls

17 lines (14 loc) · 840 Bytes

Transient and Volatile Modifiers

Transient

  • java defines two special type of modifiers transient and volatile.
  • 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
}

Volatile

  • 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 as volatile.
  • 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.