The method signature of ObjectOutputStream
's write method is
public final void writeObject(Object obj) throws IOException
As obj
should implements Serializable
(know about markers).
Why java developers do not write this method as
public final void writeObject(Serializable obj) throws IOException
is there any reason ?
If we want to serialize one object but do not want to serialize specific fields, then we can mark those fields as transient. All the static fields belong to the class instead of the object, and the serialization process serializes the object so static fields can not be serialized.
A Java object is serializable if its class or any of its superclasses implements either the java. io. Serializable interface or its subinterface, java.
If your object has changed, more than just adding simple fields to the object, it is possible that Java cannot deserialize the object correctly even if the serialization ID has not changed. Suddenly, you cannot retrieve your data any longer, which is inherently bad.
If you want a class object to be serializable, all you need to do it implement the java. io. Serializable interface. Serializable in java is a marker interface and has no fields or methods to implement.
writeObject is defined in ObjectOutput interface and its API says The class that implements this interface defines how the object is written
. It means that theoretically there may be implementations other than ObjectOutputStream which may be using other ways of serialization which do not require the object be Serializable.
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