I know what is serialization and why it is used, but my question:
writeObject
, readObject
in Serializable interface because when we do serialization we finally override these 2 methods?How does readResolve
ensure that the object created during deserialization is not the new object. I know the below and it is returning the same object during deserialization but who will call this readResolve
method internally?
private Object readResolve() throws java.io.ObjectStreamException {
return INSTANCE;
}
FileInputStream
, which relies on an open operating system file handle which might no longer exist when the object is deserialized). The modern way of having such a declaration would be an annotation, but those didn't exist in Java at the time serialization was added.Serializable
.this
). Whatever comes out of this method is returned to the code that requested the deserialization. If a preexisting object was returned, the new object created by the deserializer will not be seen by anyone and will eventually be garbage collected.Marker Interfaces are used to tell JVM to perform specific tasks. they don't have any method. Serializable is also a marker interface.
Serialization is the process of flattening the objects. when you implement serializable interface in a class, it tells JVM to serialize its object i.e. it has to be converted into stream.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With