JVM serialization ‘unexpected behavior’
Hello all,
One of my personal projects is a web site (jugandoenprimera.com) developed with Wicket framework (wicket.apache.org). This framework intensively uses the mechanism of serialization implemented in the java virtual machine to preserve pages’ state across requests.
Java also provides a keyword named transient that can be applied to an attribute and it implies that this field should not be taken into account when serialization occurs (as if it were not there).
For example:
public class MyClass {
private transient int myField = -1;
}
It turned out that I didn’t need a field to be serialized so I marked it as transient. Also, that field had an inline initialization (like in the above example).
Well, the problem raised when hydration process occurred. I expected that after hydration the value of myField were -1. But this doesn’t work like that. The value set to the transient attributes is the default for that type (0 for integers, bytes, longs, false for booleans, null for references).
This doesn’t look like as an expected behavior but led me to a hard-to-find bug.
See ya.