Transient vs Volatile modifiers in Java
By: Reema sen
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
Archived Comments
1. Tomorrow is my java's exam...ANd this is definitely gonna help ...Thank you so much sir :)
View Tutorial By: hardeep at 2013-05-28 08:51:55
2. Updated example to better show Thread T2 showing the latest updated value in variable 'testValue'.
View Tutorial By: Sanjiv at 2013-05-25 20:26:31
3. it is really best site for the information.many time i open this site but it never hopeless me.
View Tutorial By: panchanan ruata at 2013-05-22 09:36:37
4. @Reema Sen:
It would have been better if you have used your brain to post such an example bec
View Tutorial By: Mragank Singhal at 2013-02-13 04:37:06
5. I think 19 floor's code doesn't fix the problem.Although the result is what we want to see.But assum
View Tutorial By: Benson at 2012-11-28 08:04:26
6. Hi, You explanation is good.
But it may be made better with the help of examples or pictorial
View Tutorial By: Dishant at 2012-10-18 05:07:51
7. Copy and pasted from Java:The Complete Reference. Although nice selection
View Tutorial By: Imon at 2012-08-07 07:02:14
8. This is not your explanation. This is from the book, the complete reference to java. Can you yoursel
View Tutorial By: Subhaprasad Mukherjee at 2012-04-14 03:35:29
9. Straight tutorial, keep on working. Thanks
View Tutorial By: Gerson at 2012-04-09 19:42:19
10. Awesome, now I can say I've fully understood volatile. thanks
View Tutorial By: Andrew at 2012-01-13 18:19:15
11. There was no sharing of volatile variable in the program. It just used the volatile instance variabl
View Tutorial By: Rajesh Poppoppu at 2011-12-30 13:04:42
12. Transient: The transient modifier applies to variables only and it is not stored as part of its obje
View Tutorial By: Manoj Yadav at 2011-11-23 09:33:23
13. i thnk exampleThread example is wrong - like billgates says.
the TestValue instance variable
View Tutorial By: oldman at 2011-09-24 12:32:33
14. Hi there, I fixed the above (non-working) example:
package com.src.volatileExample;<
View Tutorial By: JavaBoy at 2011-09-24 05:41:19
15. Thanks Mohit ..... Clear Explanation abt Transient.
View Tutorial By: Dhi at 2011-08-22 06:28:06
16. Nice artielce , by the way just to add from my experience I think we should make a variable volatile
View Tutorial By: Javin @ eclipse remote debugging at 2011-03-11 22:00:15
17. Real Life example of using Volatile
******************************************
View Tutorial By: Mayank at 2011-01-25 15:44:21
18. What is transient keyword in Java?
What is Serilization?
If you want t
View Tutorial By: Mohit at 2010-11-18 00:26:24
19. Very good explaination of transient and volatile. Aayush reveals some important facts of volatile va
View Tutorial By: Himanshu Upadhyay at 2010-11-15 04:48:43
20. Can some body throw more light on TRANSIENT ....
View Tutorial By: Anish at 2010-08-03 07:30:58
21. Hi ,learn a new thing about These Transient and Volatile Modifiers .
Thanks to all of you.
View Tutorial By: LakshmiNarayana Golla at 2010-07-28 02:49:48
22. HI your explanation is absolutely correct but I am sure that it's copied from Complete reference Jav
View Tutorial By: Anil R. Chinchawade at 2010-07-28 02:02:01
23. This example is working fine....I think BillGates has misunderstood somewhere..
View Tutorial By: Monoj Rath at 2010-07-15 01:27:51
24. The example is wrong, because there are two seperate instances of ExampleThread, so the testValue is
View Tutorial By: billGates at 2010-07-06 16:56:13
25. Explanation is very good. Thanks all.
View Tutorial By: reddy at 2010-04-07 08:47:09
26. This is an excerpt from Java Complete Reference.Just copy and paste technology
View Tutorial By: Guest at 2010-03-25 01:36:57
27. Your explanation is good but, it will be better if you can try explain with some real java program
View Tutorial By: Jabaraj at 2010-03-22 06:02:33
28. If you are working with the multi-threaded programming, the volatile keyword will be more useful. Wh
View Tutorial By: it career at 2010-02-24 00:13:50
29. You should consider using different threads that are not instantiated from the same class, but diffe
View Tutorial By: Jeremy at 2010-01-13 23:02:11
30. If you are working with the multi-threaded programming, the volatile keyword will be more useful. Wh
View Tutorial By: Prithvi at 2010-01-07 10:24:12
31. Hello,
A volatile variable does not have a copy maintained in the local memory of the
View Tutorial By: aayush at 2009-11-16 07:38:23
32. Your complete answer is taken from Herbert Schildt's Complete Reference book. Next time try to impro
View Tutorial By: Priyanka at 2009-10-31 07:29:05
33. Hi thanks for your information.
I got simple solution for my breath taking problem.
View Tutorial By: karthikeyan at 2009-09-18 04:50:37
34. Your explanation is perfectly clear and not in need of "illustrative examples"! In particu
View Tutorial By: Percy at 2009-06-08 14:14:39
35. Your explanation is good and okay but, it will be better or most lovely only if you can try to and s
View Tutorial By: Emmanuel at 2009-05-28 02:45:11
36. I think this clarification is not sufficient..If you need perfect solution ... Kindly go throw http
View Tutorial By: Abhinav at 2009-02-10 12:35:56
Comment on this tutorial
- Data Science
- Android
- AJAX
- ASP.net
- C
- C++
- C#
- Cocoa
- Cloud Computing
- HTML5
- Java
- Javascript
- JSF
- JSP
- J2ME
- Java Beans
- EJB
- JDBC
- Linux
- Mac OS X
- iPhone
- MySQL
- Office 365
- Perl
- PHP
- Python
- Ruby
- VB.net
- Hibernate
- Struts
- SAP
- Trends
- Tech Reviews
- WebServices
- XML
- Certification
- Interview
categories
Related Tutorials
Java program to get location meta data from an image
Program using concept of byte long short and int in java
Update contents of a file within a jar file
Tomcat and httpd configured in port 8080 and 80
Count number of vowels, consonants and digits in a String in Java
Student marks calculation program in Java
Calculate gross salary in Java
Calculate average sale of the week in Java
Vector in Java - Sample Program