Wednesday, January 31, 2007

Java Serialization Review

Java Serialization is always a tricky place and a hot spot for java job interview. Two important things:
1. If the Super class is not serializable and the sub class is. Then one important requirement is that the Superclass must have a non-arg constructor accessible by the sub class. Otherwise run time exception. Also all the fields in the super class will not be restored through serialization. ( The Default value or values set in the super class constructor will be used for these fields)

2. If the serializable contains a member variable which is not Serializable. Runtime exception will throw. The easy workaround is to mark the non-serializable object as a static memeber. Static member will not be serialized and default value is used. This also reflect why we should use POJO as much as possible and avoid complex non-serializable objects. Normally these objects are Unit object and should not be included in a transfer object at all.

No comments: