Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why not java replaced Serializable with @Serializable annotation [closed]

after the introduction of annotation, why not java replaced Serializable with @Serializable annotation

like image 343
Panky031 Avatar asked Mar 30 '16 18:03

Panky031


People also ask

Is Java serialization deprecated?

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.

Why do we need serialVersionUID in Java?

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.

Is Serializable consider declaring a serialVersionUID?

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 ...

How do you stop serialization in Java?

To avoid Java serialization you need to implement writeObject() and readObject() method in your Class and need to throw NotSerializableException from those method.


1 Answers

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.

like image 85
Peter Lawrey Avatar answered Nov 11 '22 00:11

Peter Lawrey