Java serializable interface

I was trying to understand the concept behind the java serialization.

I know java serialization is required when you want to save state of an object outside JVM. My thinking was why don’t Sun make all classes as Serializable by default? Why Serializable interface required to persist an object?

I did google search to get answer for my questions.

Many of them gave definition as “It tells JVM to serialize object that are ready to transfer over outside JVM, also it make object as JVM independent.”

I am not satisfied with the above definition. I continued my search and found below understanding on serialization concept in Java.

  1. java.io.Serializable interface doesn’t add any overhead to JVM nor it does any magic in JVM processing.
  2. Using serialization concept an object can be exposed outside jvm it intern exposes private variables’ value which is violation of OOPS concept. By marking a class as Serializable, developers understand the security concern.
  3. JVM serializes complete object navigation graph. Sometime developer might intent to persist only top level object and forget to mark member variables as transient. In this case Serializable interface enforces developer to make conscious decision to persistent required classes.

java.io.Serializable interface is an design decision in Java to enforce developers to make conscious decision before exposing private variable.

Reference:
http://java.sun.com/javase/technologies/core/basic/serializationFAQ.jsp#whyserial

2 responses to “Java serializable interface”

  1. Santhosh

    Nice

  2. Gustavo

    But IO classes cannot be serializable. How do you explain that?

Leave a comment

I’m Venkat

A passionate software engineer with over 20 years of experience in the tech industry. Welcome to my digital playground where I share my journey through the ever-evolving world of software development and cloud technologies!