jQuery in Action, 2nd edition*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Transient and volatile variables ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Transient and volatile variables ?" Watch "Transient and volatile variables ?" New topic
Author

Transient and volatile variables ?

uday mane
Greenhorn

Joined: Apr 25, 2011
Posts: 3
Hi all,

I've basic knowledge of java and preparing for SCJP, I'm referring the book by kathy sierra.

I've question regarding transient and volatile variables in java. Please let me know if my understanding is correct:

Transient variables wont be stored/persisted in database, like other instance variables do. Is this correct?

Is 'transient' used only during serialization or in any other scenario ?

I dont understand the concept of 'volatile' variables. Please help with my queries or direct me if these things are discussed in other threads..

Thanks
-Uday
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3599
    
  14

Welcome to CodeRanch, Uday!

Transient variables are indeed not considered when an object is being serialized. It is usually used for variables that cache the result of a method that may be called often. For example, if you have an expensive hashCode() method, you may calculate the hash once and store it in a variable and return that value every time the hashCode() method is called after that. When the object is being serialized, the process is free to discard the value, because it can be easily be recomputed again.

Volatile variables tell the JVM that threads are not allowed to store values assigned to them into cache, but have to write it to memory immediately. It ensures that different threads of execution see the same value when they query the variable. It's a rarely used keyword, since it is only useful in very specific conditions.
Vijay Tidake
Ranch Hand

Joined: Nov 04, 2008
Posts: 146

Regarding the transient variable:

If a class has a transient variable and you are using multi threading ,then each thread must reconcile its copy of a transient variable from main memory.
If any thread using the transient variable modifies that variable then the thread must have to write it to memory immediately, and the other threads must reconcile their copy
of the variable from main memory before using it.

Thanks..


The important thing is not to stop questioning.Curiosity has its own reason for existing.
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3599
    
  14

No, that's volatile, not transient.
Vijay Tidake
Ranch Hand

Joined: Nov 04, 2008
Posts: 146

yaa thanks..

while posting reply ,may be Im thinking something else!!!

Thanks again
uday mane
Greenhorn

Joined: Apr 25, 2011
Posts: 3
Thanks for the information Stephan and Vijay

I have found one more view to understand volatile :

volatile is usually used in threads. In threading there are two types of memory. Main memory and threads’ local memory.
Threads’ local memory is like a screen shot of main memory. So screenshot is not necessarily the current state of the main memory but the state when screenshot is taken.
volatile tells the thread not use screenshot of variable but use main memory instead which is always updated.
 
 
subject: Transient and volatile variables ?
 
Similar Threads
What should I know about volatile for the exam ?
volatile and transient
meaning of keywords static, transient, volatile
transient and volatile variables
Please Help