ElenaT ElenaT - 2 months ago 11
Java Question

Why wasn't java.io.Serializable deprecated in Java 5?

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)

Answer

The interface is there so methods can be defined to accept objects of type Serializable:

public void registerObject(Serializable obj);