In scala, I think a field of a object will be static and not be serialized when serializing, but if the field is non serializable, a NotSerializableException got. Why ? Similar java code is OK.
Below is a simple example.
Thanks
Scala code:
package test
import java.io._
import java.util
object AStudent extends Serializable {
var name="Jack"
val map=new util.WeakHashMap
}
object SerializationDemo extends App {
val oos = new ObjectOutputStream(new FileOutputStream("./tmp/stu"))
println(AStudent.name)
AStudent.name=null
println(AStudent.name)
oos.writeObject(AStudent)
oos.close
}
Java code:
package test;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.WeakHashMap;
public class Stu implements Serializable {
public static String name="Jack";
public static WeakHashMap map=new WeakHashMap();
public static void main(String[] args) throws IOException {
ObjectOutputStream oos= new ObjectOutputStream(new FileOutputStream("./tmp/stu"));
oos.writeObject(new Stu());
oos.close();
}
}