In pre Java 5, there were no annotations. As a result you could not add metadata to a class.
To mark a class as serializable, you had to implement the Serializable interface (which is just that, a marker) and use further transient
keywords to mark a field as non serializable if needed, something like:
public class MyClass implements Serializable { ... private transient Bla field; ... }
Now you could theoretically make use of annotations (this is a perfect use for them) and have:
@Serializable public class MyClass { ... @Transient private Bla field; ... }
But the interface and the keyword were not deprecated and no annotation was added in Java 5 to replace them.
What are the considerations for this decision to keep the interface and the keyword?
Off course there is the issue of compatibility with pre Java 5 code, but at some point that will end (e.g. related to the new features of generics, the JLS specifies that It is possible that future versions of the Java programming language will disallow the use of raw types). So why not prepare the way for serialized annotations also?
Any thoughts? (although I would prefer concrete references :D which I was unable to find)
The proposal is that serialization be removed in a phased manner, as follows. In a JDK 7 update, possibly update 6, the Serializable , ObjectInputStream , ObjectOutputStream , and associated classes in the java.io package will be deprecated.
Serialization is brittle, it pokes into private field, violates constructor invariance, it's horrible in so many ways. The only thing appealing about it is that it's easy to use in simple use cases. That's what motivated getting it in there.
Usually used in Hibernate, JMS, JPA, and EJB, serialization in Java helps transport the code from one JVM to another and then de-serialize it there. Deserialization is the exact opposite process of serialization where the byte data type stream is converted back to an object in the memory.
The Serializable interface is present in java.io package. It is a marker interface. A Marker Interface does not have any methods and fields. Thus classes implementing it do not have to implement any methods. Classes implement it if they want their instances to be Serialized or Deserialized.
The interface is there so methods can be defined to accept objects of type Serializable:
public void registerObject(Serializable obj);
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