1
 class A extends Serializable{
      public A(){}
int x=0;
      private final transient ReentrantReadWriteLock lock = new ReentrantReadWriteLock();

    }

few Questions about deserialization: 1) Why is it required to provide default constructor in case serialization ? 2) Why field "lock" is not getting initialized after deserialization?

EDIT: forgot to add "transient" in my original post.Added it now.

pjain
  • 1,149
  • 2
  • 14
  • 28
  • `Serializable ` is an interface hence it should be `implements Serializable` – Nitin Dandriyal May 13 '15 at 08:06
  • `2) Why field "lock" is not getting initialized after deserialization?` because it's transient. – EpicPandaForce May 13 '15 at 08:34
  • @ EpicPandaForce, lock is not getting desrialized because of its transient nature. But why it is not getting "initialized" with a new object ? In actual I wanted to understand the steps of deserialization because of which it is not getting initialized? – pjain May 13 '15 at 08:43
  • I suggest you to read any java serialization tutorial to get a clear picture. Coming to your questions: 1) not at all required 2) As the lock variable is "**transient**", it is skipped while object of A gets serialized and when you de-serialize the lock variabl will be initialized to **null**. – geekprogrammer May 13 '15 at 08:53

2 Answers2

1

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.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
1
  1. Not required
  2. Lock gets initialized, below code does print the initialized lock(Just writing the Object of B in a byte array and reading back from it)

    public class B implements Serializable {
        private int x = 0;
        private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
    
        public static void main(String[] args) throws IOException, ClassNotFoundException {
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            try (ObjectOutput out = new ObjectOutputStream(bos)) {
                 out.writeObject(new B());
            }
            catch (IOException e) {
                e.printStackTrace();
                throw e;
            }
            byte[] bytes = bos.toByteArray();
    
            ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
            try (ObjectInput in = new ObjectInputStream(bis)) {
                B b = (B) in.readObject();
                System.out.println(b.lock);
            }
            catch (IOException e) {
                e.printStackTrace();
                throw e;
            }
          }
       }
    }
    
Nitin Dandriyal
  • 1,559
  • 11
  • 20