2 Answers. A volatile is typically serialized by Java Object Serialization. The threading / JMM properties of volatile are orthogonal to serialization. Incidentally, it is possible to serialize a transient or static variable, if you code custom writeObject / readObject methods to do this.
How does the volatile modifier to a data type affect serialization?
The volatile keyword flushes the changes directly to the main memory instead of the CPU cache. On the other hand, the transient keyword is used during serialization. Fields that are marked as transient can not be part of the serialization and deserialization. … Volatile are not initialized with a default value.
What kind of variables Cannot be serialized?
A static variable cannot be serialized. While de-serializing a value can be available for Static variables if the same is provided while initialization of the base class.
Is volatile variable in Java thread safe?
Reading or writing a volatile variable does not block threads reading or writing. For this to happen you must use the synchronized keyword around critical sections. 2) As an alternative to a synchronized block you could also use one of the many atomic data types found in the java.
What happens when a variable is marked as volatile?
When we declare a variable volatile, it means that all reads and all writes to this variable or from this variable will go directly into the main memory. The values of these variables will never be cached.
What is volatile modifier?
The volatile modifier is used to let the JVM understand that a thread accessing the variable should always merge its own personal copy of the variable with the original in the memory. … Volatile can only be applied to instance variables, which are of type object or private.
What is purpose of serialization of an object?
Serialization is the process of converting an object into a stream of bytes to store the object or transmit it to memory, a database, or a file. Its main purpose is to save the state of an object in order to be able to recreate it when needed.
Can we serialize final variables?
transient and final : final variables are directly serialized by their values, so there is no use/impact of declaring final variable as transient.
Can we serialize static variable?
In Java, serialization is a concept using which we can write the state of an object into a byte stream so that we can transfer it over the network (using technologies like JPA and RMI). But, static variables belong to class therefore, you cannot serialize static variables in Java.
How can you prevent a variable from being serialized?
You can prevent member variables from being serialized by marking them with the NonSerialized attribute as follows. If possible, make an object that could contain security-sensitive data nonserializable. If the object must be serialized, apply the NonSerialized attribute to specific fields that store sensitive data.
Is HashMap thread safe?
And, importantly, HashMap is not a thread-safe implementation, while Hashtable does provide thread-safety by synchronizing operations. Even though Hashtable is thread safe, it is not very efficient. Another fully synchronized Map, Collections.
Why volatile is used in Singleton?
The volatile prevents memory writes from being re-ordered, making it impossible for other threads to read uninitialized fields of your singleton through the singleton’s pointer.
What is the difference between static and volatile variable?
1. static variable is used for global variable which has used for update the value without changing stored into global part of memory. 2. volatile is used when the value of variable is updated by external factor (e.g. interrupts in micro-controller) and compiler is not optimized the volatile variable.