Wednesday, October 30, 2013

Can someone explain what the transient and volatile modifiers mean in Java?
share|improve this question
1 
This was clearly not a question and this is a duplicate anyway (which is why I voted to close). See for example Why does Java have transient variables? and When exactly do you use the volatile keyword in Java? (and the many others). –  Pascal Thivent Aug 23 '10 at 7:56 
 
possible duplicate of For what is used "volatile" ? –  Jeff Aug 27 '10 at 12:08

closed as not a real question by cherouvimhartoPascal ThiventJeff,ho1 Aug 27 '10 at 12:16

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.If this question can be reworded to fit the rules in the help center, please edit the question.

2 Answers

The volatile and transient access modifiers apply to fields of classes.
The transient modifier tells the Java object serialization subsystem to exclude the field when serializing an instance of the class. When the object is then deserialized, the field will be initialized to the default value; i.e. null for a reference type, and zero or false for a primitive type. Note that the JLS (see 8.3.1.3) does not say what transient means, but defers to the Java Object Serialization Specification. Other non-standard serialization mechanisms may also pay attention to a field'stransient-ness.
The volatile modifier tells the JVM that writes to the field should always be synchronously flushed to memory, and that reads of the field should always read from memory. This means that fields marked as volatile can be safely accessed and updated in a multi-thread application without using native or standard library-based synchronization. (This does not apply to long or double fields, which may be non-atomic on some JVMs. However, it always applies to reference-typed fields.) The relevant parts of the JLS are 8.3.1.417.4 and 17.7.
share|improve this answer
 
Transient: The transient modifier applies to variables only and it is not stored as part of its object’s Persistent state. Transient variables are not serialized. Volatile: Volatile modifier applies to variables only and it tells the compiler that the variable modified by volatile can be changed unexpectedly by other parts of the program. – Vijay Bhaskar Semwal Aug 26 '10 at 6:18
 
@Vijay - Incorrect. Transient variables could be serialized, depending on what serialization mechanism is used. The Java Language Specification does not specify this. Your definition of volatile is also incorrect. Both volatile and non-volatile variables can be changed unexpectedly by some other part of a program. Volatile simply provides a stricter semantic for read and write with respect to the Java memory model. –  Stephen C Aug 26 '10 at 7:40 
 
@Vijay - also transient and volatile only apply to fields of classes. They do not apply to local variables. – Stephen C Aug 26 '10 at 7:43 
 
i was talking about without making it serialized i.e. default transient variable are not serialized suppose we have 10 variable we have to serialized only 4 then we will declare transient rest 6 variable to make non serialized... – Vijay Bhaskar Semwal Aug 27 '10 at 11:41
 
Thanks for the explanation. But it would be better if you can give an example of for other understanding. – Vijay Bhaskar Semwal Aug 27 '10 at 11:52

No comments: