after the introduction of annotation, why not java replaced Serializable with @Serializable annotation
So far, no JDK Enhancement Proposal (JEP) has been proposed publicly to deprecate the serialization mechanism in Java 11, which is expected to be released later this year.
The SerialVersionUID must be declared as a private static final long variable in Java. This number is calculated by the compiler based on the state of the class and the class attributes. This is the number that will help the JVM to identify the state of an object when it reads the state of the object from a file.
However, it is strongly recommended that all serializable classes explicitly declare serialVersionUID values, since the default serialVersionUID computation is highly sensitive to class details that may vary depending on compiler implementations, and can thus result in unexpected InvalidClassExceptions during ...
To avoid Java serialization you need to implement writeObject() and readObject() method in your Class and need to throw NotSerializableException from those method.
Annotations are not inherited. The Serializable interface is inherited. This means that not only would be break compatibility to remove the interface, it might not be possible to replace it with an annotation as the behaviour is not the same.
Another difference is you can write
void method(Serializable s)
but you cannot do the same for an annotation, although ObjectOutputStream.writeObject takes an Object
in any case.
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