Why is the Serializable interface empty in JDK source code?

Asked

Viewed 101 times

1

I know what serialization is and what use it is. I also know when not to use it. I’ve read item 74 of the Java Effect. My question is, what is the purpose of an empty interface, in which there is nothing to be implemented?

package java.io;
public interface Serializable {
}

Would it be just like a kind of markup? If so, when would the classes "marked" as serializable be used? And what would be the name of this marking strategy?

  • @Carlosheuberger, what is "identifying the semantics of what is being serialized"?

  • 1

    I reopened because the question that was marked as duplicate does not have the information asked here, I searched a lot, I asked other people and talks about the Serializable, not because it is empty. It is only related.

1 answer

1


I couldn’t tell you why they chose to do this, maybe even they don’t know and question the decision anymore, but your assumption is the most obvious explanation. She’s got a name, it’s called Marker interface and it serves only to inform that certain classes can do something, but the implementation of this is not in the class. Almost always is a mistake and has better option.

Personally I prefer a custom attribute annotation, something like @Serializable. any code or external tool can "look" at the class and know that it can or cannot do something, just use creativity. And precisely because it shows that it has a capacity and does not have the capacity within it should not be an interface.

  • I imagine it is an interface because it predates Java 1.5 when the language started to have the Annotations.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.