1) For "System.out.println("After Deserialization collarSize value :" + d.getCollar().getCollarSize());"
The output is 0, expecting it to be 5
2) For "System.out.println("After Deserialization dogSize value :" + d.dogSize);" it gives error "java.lang.NullPointerException at SerializeCat2.main(SerializeCat2.java:97)
SCJP 6
Why to worry about things in which we dont have control, Why to worry about things in which we have control ! !
the output would have been 5, if you used the value that you passed to the method.What did you do with the size variable???
public Dog (Collar collar, int size){
theCollar =collar;
}
When deserialized, the value of transient variable of a serializble class is always set to its default value.
transient private Collar theCollar;
So, when the d.getCollar() of d.getCollar().getCollarSize(); returns,
it would be returning a null value.
Then build the the Dog object again Dogsize 5 and collarSize 3 (new Collar created durinf deserializtion.
SCJP 6
Why to worry about things in which we dont have control, Why to worry about things in which we have control ! !
Did you see how Paul cut 87% off of his electric heat bill with 82 watts of micro heaters? |