Search code examples
scalaserializablecase-class

What is the point of Serializable interface?


Here is a runnable demo

import java.io._

object Main extends App {
    case class Value(s: String)
    val serializer = new ObjectOutputStream(new ByteArrayOutputStream())
    serializer.writeObject(Value("123"))
    println("success")                        //> success

}

Please note that program succeeds despite I do not mark my class with Serializable. Does Serializable make sense in Scala?


Solution

  • Case classes extend Serializable by default in Scala. If you create a regular class it will need to extend Serializable otherwise it will throw a serialization error.