int aInt = 7;
}
public class Main implements Serializable {
int bInt = 5;
AnotherClass aAnotherClass = new AnotherClass();
public static void main(String[] args) throws Exception {
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("Main.ser"));
Main aMain = new Main();
System.out.println("" + aMain.bInt + aMain.aAnotherClass.aInt);
oos.writeObject(aMain);
oos.close();
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("Main.ser"));
aMain = (Main) ois.readObject();
ois.close();
System.out.println("" + aMain.bInt + aMain.aAnotherClass.aInt);
}
}
run:
57
Exception in thread "main" java.io.NotSerializableException: javaapplication35.AnotherClass
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1156)
at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1509)
at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1474)
at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1392)
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1150)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:326)
at javaapplication35.Main.main(Main.java:30)
Java Result: 1
As you can see it is not possible to serialize a reference to an object that is NOT serializable !
No comments:
Post a Comment