Actually I want to know about the concept of serialization and in what way that the transient&local variables are related to serialversionuid and how the serialization is actually happening?
-
1Welcome to StackOverflow. This site is not a tutorial or discussion site. You are asked to post specific questions that can be answered concisely, not open-ended requests to be taught programming concepts. Please read the [FAQ] and [Ask] – Jim Garrison Aug 25 '14 at 04:22
2 Answers
Basically, no. The serialVersionUID
is usually used to determine which version(s) of the class are compatible with each other (usually in) clustered environments, if you wanted to support multiple versions of your application in any context you might increment this field to prevent previous versions of your application from blowing up on new fields.
Fields marked with transient
are not serialized.
Finally, volatile
is used to prevent un-safe optimizations from the compiler when multiple threads can access a field. For example, double checked-locking. From the wikipedia,
As of J2SE 5.0, this problem has been fixed. The volatile keyword now ensures that multiple threads handle the singleton instance correctly. This new idiom is described in [4]:
// Works with acquire/release semantics for volatile // Broken under Java 1.4 and earlier semantics for volatile class Foo { private volatile Helper helper; public Helper getHelper() { Helper result = helper; if (result == null) { synchronized(this) { result = helper; if (result == null) { helper = result = new Helper(); } } } return result; } // other functions and members... }

- 1
- 1

- 198,278
- 20
- 158
- 249
The transient keyword in Java is used to indicate that a field should not be serialized. From the Java Language Specification, Java SE 7 Edition, Section 8.3.1.3. transient Fields:
Variables may be marked transient to indicate that they are not part of the persistent state of an object. While local variables can be serialized
Surprisingly, the java compiler does not complaint if you declare a static member field as transient or a final member field as transient in your classes. These should be compile-time errors. Because a "transient" part of an object's state is assumed to be changing within each instance, it can not be static or final
The serialization runtime associates with each serializable class a version number, called a serialVersionUID, which is used during deserialization to verify that the sender and receiver of a serialized object have loaded classes for that object that are compatible with respect to serialization. If the receiver has loaded a class for the object that has a different serialVersionUID than that of the corresponding sender's class, then deserialization will result in an InvalidClassException. A serializable class can declare its own serialVersionUID explicitly by declaring a field named "serialVersionUID" that must be static, final, and of type long: ANY-ACCESS-MODIFIER static final long serialVersionUID = 42L; If a serializable class does not explicitly declare a serialVersionUID, then the serialization runtime will calculate a default serialVersionUID value for that class based on various aspects of the class, as described in the Java(TM) Object Serialization Specification. However, it is strongly recommended that all serializable classes explicitly declare serialVersionUID values, since the default serialVersionUID computation is highly sensitive to class details that may vary depending on compiler implementations, and can thus result in unexpected InvalidClassExceptions during deserialization. Therefore, to guarantee a consistent serialVersionUID value across different java compiler implementations, a serializable class must declare an explicit serialVersionUID value. It is also strongly advised that explicit serialVersionUID declarations use the private modifier where possible, since such declarations apply only to the immediately declaring class--serialVersionUID fields are not useful as inherited members.
Java docs says:
"the default serialVersionUID computation is highly sensitive to class details that may vary depending on compiler implementations, and can thus result in unexpected InvalidClassExceptions during deserialization".
You must declare serialVersionUID because it give us more control.

- 896
- 6
- 21