Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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)

like image 372
ElenaT Avatar asked Jun 17 '11 20:06

ElenaT


People also ask

Is Java serialization deprecated?

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.

What is wrong with Java serialization?

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.

Is serialization still used in Java?

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.

What is Java IO serializable?

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.


1 Answers

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

public void registerObject(Serializable obj); 
like image 164
Ethan Cabiac Avatar answered Sep 19 '22 14:09

Ethan Cabiac