I cannot understand the basics of Serialization in Java 1.6.
The following is the Dog class containing the instance variable Collar Class:
Dog.java
public class Dog implements Serializable { private Collar collar; public Collar getCollar() { return collar; } public void setCollar(Collar collar) { this.collar = collar; } }
The Collar class does not implement the Serializable interface, as shown below:
Collar.java
public class Collar { private int size; public int getSize() { return size; } public void setSize(int size) { this.size = size; } }
Now when I try to serialize Dog , why doesn't it throw a NotSerializableException ? Under the contract, the whole graphic object should implement Serializable , but my Collar class does not do this.
The following is the main method for this demo:
public static void main(String[] args) { try { FileOutputStream fs = new FileOutputStream("E:\\test.ser"); ObjectOutputStream os = new ObjectOutputStream(fs); Dog dog = new Dog();
Please explain this, I'm completely confused, trying to implement all the theoretical materials :(
source share