• Post Reply Bookmark Topic Watch Topic
  • New Topic

Use of Transient and Volatile modifiers?  RSS feed

 
Megha Jain
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can anybody tell me what is transient and volatile modifier clearly.......
I have read that when a varible is declared as transient, then it's vaue need not persist when an object is stored............ but is didn't get it clearly..... I will really appreciate if anybody can explain me this by giving an example........ and it's use.....
Silimarly use of volatile modifier is also not clear to me.........
Answer me Soon................
 
Julien Grenier
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
The quick answer is : google and java.sun.com.
The long answer is (from memory):

The transient modifier is only useful when you are serializing the object(search for info about serialization). It means that the transient field won't be save. so usualy it's for certain irrevelant field that you don;t necessary want when you will deserialized the object.

The volatile modifier is useful in multi-threaded enverionment because it assure that the value of the field will always be up-to-date. example : 2 thread are modifying a counter on the same object. if that counter is not volatile and the method not synchronized the result can be unpredictable but if counter is volatile the value can be determined because (if i'm correct) the value of the variable is not cache for each thread.(so it's a bit slower I do think ... not sure).

ps. : I'm probably wrong.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!