Transient and volatile modifier (Most asked in Interviews)

Posted by


The volatile keyword is used on variables that may be modified simultaneously by other threads. This warns the compiler to fetch them fresh each time, rather than caching them in registers. This also inhibits certain optimizations that assume no other thread will change the values unexpectedly. Since other threads cannot see local variables, there is never any need to mark local variables volatile.
                           It means that, in threaded programs, the “base” version of the variable will be checked before every access to the thread local version. It is just one more way of ensuring consistent values between threads (but should not be used alone, it is not a replacement for synchronization).A volatile variable does not have a copy maintained in the local memory of the thread (on the stack). All changes to the volatile variable (caused by multiple threads) are flushed out to the heap memory (visible from all threads). Hence volatile variable values remain consistent for all threads

/*** Note that the volatile and final keywords are not used on the same variable.
* volatile and transient keywords are used on a variable, together as though
* something could be transient and volatile, which they can. The word volatile
* origninally came from work where a device may change a variable, independent
* of the code. transient says to the compiler, do not save this in serialization.
public class TransientDemo
private static transient final Long startTime = new Long(currentTimeMillis());
public static volatile transient long anotherTimeMillis;

 On the other hand, for other instance variables, each java thread maintains a local copy on the stack. Multiple threads may modify this local copy of the instance variable and hence inconsistent values may be visible for multiple threads.
 For preventing this condition, we synchronize. During synchronization, a lock is first taken on the object monitor. Then the thread reads the state from the main memory and flushes its internal state. Subsequently, the synchronized code block/method code is executed. Once the execution completes, all the changes to the variables of that thread are flushed out to the main memory. Then the object monitor lock is released.
 So, as we can see, volatile is a specialized case of synchronization. The only exceptions are, that it operates on a single field and no locks on the object monitor are required (as it operates on the heap memory and not the thread local stack memory).

             Transient means that when an object is serialized this variable will not be serialized with it.If you mark an instance variable as transient you’re telling the JVM  to skip this variable when you attempt to serialize the object containing it.
In serialization object, some field is marked as transient,will not be transmitted in the stream for example file handles,database connections etc. Such objects are only meaningful locally.


class Table implements serializable{
String color=null;
transient File fh=null;

after serialization, it can be

class Table implements serializable{
String color=null;



Leave a Reply