Example & Tutorial understanding programming in easy ways.

What is the Difference between volatile and transient keyword in Java?


 1) transient keyword is used along with instance variables to exclude them from serialization process. if a field is transient its value will not be persisted.On the other hand volatile keyword can also be used in variables to indicate compiler and JVM that always read its value from main memory and follow happens-before relationship on visibility of volatile variable among multiple thread.
 
 2) transient keyword can not be used along with static keyword but volatile can be used along with static.
 
 3) transient variables are initialized with default value during de-serialization and there assignment or restoration of value has to be handled by application code.
  

Read More →