1) Why is it required to provide default constructor in case serialization.
It isn't a requirement. Or at least, it isn't a requirement if you use serialization as implemented using ObjectInputStream
and ObjectOutputStream
.
2) Why field "lock" is not getting initialized after deserialization?
It should be initialized ... if you are using ObjectInputStream
and ObjectOutputStream
. The lock
object should be serialized and then deserialized.
(If you have a case where lock
apparently isn't being initialized, then please post an SSCCE to show what is happening.)
I incorrectly asserted that ReentrantReadWriteLock
isn't serializable ... It is.
UPDATE in the version with transient
. The lock
field for a deserialized instance of A
will be null
. The initialization expression in the declaration of lock
doesn't get executed.
For more information, please read the Java Object Serialization Specification.
If you are using some other serialization mechanism, you need to say what it is.