-1
 private void writeObject(ObjectOutputStream o)
        throws IOException {  
        o.writeObject(propertyTwo);

        o.defaultWriteObject();  
  }

        private void readObject(ObjectInputStream o)
        throws IOException, ClassNotFoundException {

        propertyTwo = (String) o.readObject();
        o.defaultReadObject();
  }

Please explain how it serialize transient variable if we use defaultWriteObject and writeObject together?

2
  • You cant serialize transient variables. Please read this post and accociated links Commented Jul 5, 2017 at 8:02
  • @LaxmanChari You can, and this post shows exactly how. Commented Jul 5, 2017 at 9:58

1 Answer 1

0

It serializes it because you serialized it. Nothing to do with defaultWriteObject() or defaultReadObject() whatsoever. They won't serialize transient members. But you did.

Sign up to request clarification or add additional context in comments.

3 Comments

But as we know f any variable is marked transient ,then that can not be serialized.
Is not serialized by default, unless you serialise it yourself, which is exactly what you have done. 'Cannot be serialized' is not correct.
Thanks,got it now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.