title: Even trivial serialization examples in Scala don't work. Why?
body: <p>I am trying the simplest possible serialization examples of a class:</p>
  
  <pre><code>@serializable class Person(age:Int) {}
  val fred = new Person(45)
  import java.io._
  val out = new ObjectOutputStream(new FileOutputStream("test.obj"))
  out.writeObject(fred)
  out.close()
  </code></pre>
  
  <p>This throws exception "java.io.NotSerializableException: Main$$anon$1$Person" on me. Why?
  Is there a simple serialization example?
  I also tried</p>
  
  <pre><code>@serializable class Person(nm:String) {
      private val name:String=nm
  }
  val fred = new Person("Fred")
  ...
  </code></pre>
  
  <p>and tried to remove <code>@serializable</code> and some other permutations. The file "test.obj" is created, over 2Kb in size and has plausible contents.</p>
  
  <h1>EDIT:</h1>
  
  <p>Reading the "test.obj" back in (from the 2nd answer below) causes</p>
  
  <blockquote>
    <p>Welcome to Scala version 2.10.3 (Java HotSpot(TM) 64-Bit Server VM,
    Java 1.7.0_51). Type in expressions to have them evaluated. Type :help
    for more information.</p>
    
    <p>scala> import java.io._ import java.io._</p>
    
    <p>scala> val fis = new FileInputStream( "test.obj" ) fis:
    java.io.FileInputStream = java.io.FileInputStream@716ad1b3</p>
    
    <p>scala> val oin = new ObjectInputStream( fis ) oin:
    java.io.ObjectInputStream = java.io.ObjectInputStream@1f927f0a</p>
    
    <p>scala> val p= oin.readObject java.io.WriteAbortedException: writing
    aborted; java.io.NotSerializableException: Main$$anon$1
            at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1354)
            at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:1990)
            at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1915)
            at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1798)
            at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1350)
            at java.io.ObjectInputStream.readObject(ObjectInputStream.java:370)
            at .(:12)
            at .()
            at .(:7)
            at .()
            at $print()
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
            at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
            at java.lang.reflect.Method.invoke(Method.java:606)
            at scala.tools.nsc.interpreter.IMain$ReadEvalPrint.call(IMain.scala:734)
            at scala.tools.nsc.interpreter.IMain$Request.loadAndRun(IMain.scala:983)
            at scala.tools.nsc.interpreter.IMain.loadAndRunReq$1(IMain.scala:573)
            at scala.tools.nsc.interpreter.IMain.interpret(IMain.scala:604)
            at scala.tools.nsc.interpreter.IMain.interpret(IMain.scala:568)
            at scala.tools.nsc.interpreter.ILoop.reallyInterpret$1(ILoop.scala:756)
            at scala.tools.nsc.interpreter.ILoop.interpretStartingWith(ILoop.scala:801)
            at scala.tools.nsc.interpreter.ILoop.command(ILoop.scala:713)
            at scala.tools.nsc.interpreter.ILoop.processLine$1(ILoop.scala:577)
            at scala.tools.nsc.interpreter.ILoop.innerLoop$1(ILoop.scala:584)
            at scala.tools.nsc.interpreter.ILoop.loop(ILoop.scala:587)
            at scala.tools.nsc.interpreter.ILoop$$anonfun$process$1.apply$mcZ$sp(ILoop.scala:878)
            at scala.tools.nsc.interpreter.ILoop$$anonfun$process$1.apply(ILoop.scala:833)
            at scala.tools.nsc.interpreter.ILoop$$anonfun$process$1.apply(ILoop.scala:833)
            at scala.tools.nsc.util.ScalaClassLoader$.savingContextLoader(ScalaClassLoader.scala:135)
            at scala.tools.nsc.interpreter.ILoop.process(ILoop.scala:833)
            at scala.tools.nsc.MainGenericRunner.runTarget$1(MainGenericRunner.scala:83)
            at scala.tools.nsc.MainGenericRunner.process(MainGenericRunner.scala:96)
            at scala.tools.nsc.MainGenericRunner$.main(MainGenericRunner.scala:105)
            at scala.tools.nsc.MainGenericRunner.main(MainGenericRunner.scala) Caused
    by: java.io.NotSerializableException: Main$$anon$1
            at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1183)
            at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1547)
            at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1508)
            at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1431)
            at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1177)
            at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:347)
            at Main$$anon$1.(a.scala:11)
            at Main$.main(a.scala:1)
            at Main.main(a.scala)
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
            at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
            at java.lang.reflect.Method.invoke(Method.java:606)
            at scala.tools.nsc.util.ScalaClassLoader$$anonfun$run$1.apply(ScalaClassLoader.scala:71)
            at scala.tools.nsc.util.ScalaClassLoader$class.asContext(ScalaClassLoader.scala:31)
            at scala.tools.nsc.util.ScalaClassLoader$URLClassLoader.asContext(ScalaClassLoader.scala:139)
            at scala.tools.nsc.util.ScalaClassLoader$class.run(ScalaClassLoader.scala:71)
            at scala.tools.nsc.util.ScalaClassLoader$URLClassLoader.run(ScalaClassLoader.scala:139)
            at scala.tools.nsc.CommonRunner$class.run(ObjectRunner.scala:28)
            at scala.tools.nsc.ObjectRunner$.run(ObjectRunner.scala:45)
            at scala.tools.nsc.CommonRunner$class.runAndCatch(ObjectRunner.scala:35)
            at scala.tools.nsc.ObjectRunner$.runAndCatch(ObjectRunner.scala:45)
            at scala.tools.nsc.ScriptRunner.scala$tools$nsc$ScriptRunner$$runCompiled(ScriptRunner.scala:171)
            at scala.tools.nsc.ScriptRunner$$anonfun$runScript$1.apply(ScriptRunner.scala:188)
            at scala.tools.nsc.ScriptRunner$$anonfun$runScript$1.apply(ScriptRunner.scala:188)
            at scala.tools.nsc.ScriptRunner$$anonfun$withCompiledScript$1.apply$mcZ$sp(ScriptRunner.scala:157)
            at scala.tools.nsc.ScriptRunner$$anonfun$withCompiledScript$1.apply(ScriptRunner.scala:131)
            at scala.tools.nsc.ScriptRunner$$anonfun$withCompiledScript$1.apply(ScriptRunner.scala:131)
            at scala.tools.nsc.util.package$.trackingThreads(package.scala:51)
            at scala.tools.nsc.util.package$.waitingForThreads(package.scala:35)
            at scala.tools.nsc.ScriptRunner.withCompiledScript(ScriptRunner.scala:130)
            at scala.tools.nsc.ScriptRunner.runScript(ScriptRunner.scala:188)
            at scala.tools.nsc.ScriptRunner.runScriptAndCatch(ScriptRunner.scala:201)
            at scala.tools.nsc.MainGenericRunner.runTarget$1(MainGenericRunner.scala:76)
            ... 3 more</p>
  </blockquote>