Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implementing Serializable without providing writeObject / readObject method

What if a class is implementing serializable interface, but there's no writeObject/readObject method implementation anywhere in the codebase?

Will the default methods defaultWriteObject/defaultReadObject do the serialization or not?

Is only marking the class with implements Serializable enough to serialize a class?

If yes, then what is getting serialized and where is the state of object getting persisted?

like image 422
MooG Avatar asked Dec 21 '15 07:12

MooG


1 Answers

What if a class is implementing serializable interface, but there's no writeObject/readObject method implementation anywhere in the codebase?

It will be subject to default serialization: see below.

Will the default methods defaultWriteObject/defaultReadObject do the serialization or not?

No, because they won't be invoked unless you invoke them.

Is only marking the class with implements Serializable enough to serialize a class?

Yes, if you're content with default serialization: see below.

If yes, then what is getting serialized

All the non-transient non-static member variables of the class and all its base classes that implement Serializable, and nothing else.

and where is the state of object getting persisted?

Into the stream. This part of your question doesn't seem to make sense,

like image 69
user207421 Avatar answered Sep 24 '22 22:09

user207421