Transient vs Volatile modifiers in Java

By: Reema sen Viewed: 165527 times  Printer Friendly Format    


Java defines two interesting type modifiers: transient and volatile. These modifiers are used to handle somewhat specialized situations.

When an instance variable is declared as transient, then its value need not persist when an object is stored. For example:

class T {
transient int a; // will not persist
int b; // will persist
}

Here, if an object of type T is written to a persistent storage area, the contents of a would not be saved, but the contents of b would. The volatile modifier tells the compiler that the variable modified by volatile can be changed unexpectedly by other parts of your program. One of these situations involves multithreaded programs.  In a multithreaded program, sometimes, two or more threads share the same instance variable. For efficiency considerations, each thread can keep its own, private copy of such a shared variable. The real (or master) copy of the variable is updated at various times, such as when a synchronized method is entered. While this approach works fine, it may be inefficient at times. In some cases, all that really matters is that the master copy of a variable always reflects its current state. To ensure this, simply specify the variable as volatile, which tells the compiler that it must always use the master copy of a volatile variable (or, at least, always keep any private copies up to date with the master copy, and vice versa). Also, accesses to the master variable must be executed in the precise order in which they are executed on any private copy.

Note: volatile in Java has, more or less, the same meaning that it has in C/C++.

This is an extract from the book 'Java Complete Reference'. For more details please refer to the book



Most Viewed Articles (in Java )

Latest Articles (in Java)

Comment on this tutorial

Subscribe to Tutorials

Related Tutorials

Archived Comments

1. I think this clarification is not sufficient..If y
View Tutorial          By: Abhinav at 2009-02-10 12:35:56

2. Your explanation is good and okay but, it will be
View Tutorial          By: Emmanuel at 2009-05-28 02:45:11

3. Your explanation is perfectly clear and not in nee
View Tutorial          By: Percy at 2009-06-08 14:14:39

4. Hi thanks for your information.

I g

View Tutorial          By: karthikeyan at 2009-09-18 04:50:37

5. Your complete answer is taken from Herbert Schildt
View Tutorial          By: Priyanka at 2009-10-31 07:29:05

6. Hello,

A volatile variable does not

View Tutorial          By: aayush at 2009-11-16 07:38:23

7. If you are working with the multi-threaded program
View Tutorial          By: Prithvi at 2010-01-07 10:24:12

8. You should consider using different threads that a
View Tutorial          By: Jeremy at 2010-01-13 23:02:11

9. If you are working with the multi-threaded program
View Tutorial          By: it career at 2010-02-24 00:13:50

10. Your explanation is good but, it will be better i
View Tutorial          By: Jabaraj at 2010-03-22 06:02:33

11. This is an excerpt from Java Complete Reference.Ju
View Tutorial          By: Guest at 2010-03-25 01:36:57

12. Explanation is very good. Thanks all.
View Tutorial          By: reddy at 2010-04-07 08:47:09

13. The example is wrong, because there are two sepera
View Tutorial          By: billGates at 2010-07-06 16:56:13

14. This example is working fine....I think BillGates
View Tutorial          By: Monoj Rath at 2010-07-15 01:27:51

15. HI your explanation is absolutely correct but I am
View Tutorial          By: Anil R. Chinchawade at 2010-07-28 02:02:01

16. Hi ,learn a new thing about These Transient and Vo
View Tutorial          By: LakshmiNarayana Golla at 2010-07-28 02:49:48

17. Can some body throw more light on TRANSIENT ....
View Tutorial          By: Anish at 2010-08-03 07:30:58

18. Very good explaination of transient and volatile.
View Tutorial          By: Himanshu Upadhyay at 2010-11-15 04:48:43

19. What is transient keyword in Java?


View Tutorial          By: Mohit at 2010-11-18 00:26:24

20. Real Life example of using Volatile
*******

View Tutorial          By: Mayank at 2011-01-25 15:44:21

21. Nice artielce , by the way just to add from my exp
View Tutorial          By: Javin @ eclipse remote debugging at 2011-03-11 22:00:15

22. Thanks Mohit ..... Clear Explanation abt Transien
View Tutorial          By: Dhi at 2011-08-22 06:28:06

23. Hi there, I fixed the above (non-working) example
View Tutorial          By: JavaBoy at 2011-09-24 05:41:19

24. i thnk exampleThread example is wrong - like billg
View Tutorial          By: oldman at 2011-09-24 12:32:33

25. Transient: The transient modifier applies to varia
View Tutorial          By: Manoj Yadav at 2011-11-23 09:33:23

26. There was no sharing of volatile variable in the p
View Tutorial          By: Rajesh Poppoppu at 2011-12-30 13:04:42

27. Awesome, now I can say I've fully understood volat
View Tutorial          By: Andrew at 2012-01-13 18:19:15

28. Straight tutorial, keep on working. Thanks
View Tutorial          By: Gerson at 2012-04-09 19:42:19

29. This is not your explanation. This is from the boo
View Tutorial          By: Subhaprasad Mukherjee at 2012-04-14 03:35:29

30. Copy and pasted from Java:The Complete Reference.
View Tutorial          By: Imon at 2012-08-07 07:02:14

31. Hi, You explanation is good.
But it may be

View Tutorial          By: Dishant at 2012-10-18 05:07:51

32. I think 19 floor's code doesn't fix the problem.Al
View Tutorial          By: Benson at 2012-11-28 08:04:26

33. @Reema Sen:
It would have been better if yo

View Tutorial          By: Mragank Singhal at 2013-02-13 04:37:06

34. it is really best site for the information.many ti
View Tutorial          By: panchanan ruata at 2013-05-22 09:36:37

35. Updated example to better show Thread T2 showing t
View Tutorial          By: Sanjiv at 2013-05-25 20:26:31

36. Tomorrow is my java's exam...ANd this is definitel
View Tutorial          By: hardeep at 2013-05-28 08:51:55