public class MySerializable implements Serializable{
private int x=10;
private static int y = 15;
public static void main(String...args){
AnotherClass a = new AnotherClass();
AnotherClass b;
//Serialize
try {
FileOutputStream fout = new FileOutputStream("MyFile.ser");
ObjectOutputStream Oout = new ObjectOutputStream(fout);
Oout.writeObject(a);
System.out.println( a.toString());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
//De-serialize
try {
FileInputStream fis = new FileInputStream("MyFile.ser");
ObjectInputStream Oin = new ObjectInputStream (fis);
b = (AnotherClass) Oin.readObject();
System.out.println( b.toString());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}catch (ClassNotFoundException e){
e.printStackTrace();
}
}
}
class AnotherClass implements Serializable{
transient int x = 8;
static int y = 9;
@Override
public String toString() {
return "x : " + x + ", y :" + y;
}
}
你能告诉我静态变量是如何序列化的吗?