What is the use of volatile and transient modifiers?

Questions by baabuma

Showing Answers 1 - 2 of 2 Answers

JavaStudent

  • Feb 12th, 2007
 

Transient: The transient modifier applies to variables only and it is not stored as part of its object's Persistent state. These variables are not serialized. Transient instance fields are neither saved nor restored by the standard serialization . You have to handle restoring them yourself.Volatile: Volatile modifier tells the compiler that the variable modified by volatile can be changed unexpectedly by other parts of the program. For example a Variable might be read from Cache and not update the content if it has been changed by another thread. Specifying a variable as volatile tells the JVM that any threads using that variable are not allowed to cache thatvalue at all. Making the Variable Volatile will ensure that the compiler will get the content of the variable every time it is used and not cache its content. If not used Carefully this modifier might introduce bugs.

  Was this answer useful?  Yes

Sunny Jain

  • Feb 27th, 2007
 

Volatile Variable: Whenever a variable is declared as volatile, the thread accessing the variable have to reconcile its copy of variable with the master copy of that variable....

Transient:
We can declare any instance variable as transient variable..all it does is, it asks the JVM to skip this variable while serializing the object.

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions