I need to apply Serialization to a big java project. There are quite some classes which use a non-static inner class.
I know that inner classes hold an implicit reference to the outer class. In my case, I would like to Serialize/deserialize both the outer class, AND the inner class instances.
Generally, Serialization of non-static inner classes is strongly discouraged. From the documentation:
Serialization of inner classes, including local and anonymous classes, is strongly discouraged. When the Java compiler compiles certain constructs, such as inner classes, it creates synthetic constructs; these are classes, methods, fields, and other constructs that do not have a corresponding construct in the source code. Synthetic constructs enable Java compilers to implement new Java language features without changes to the JVM. However, synthetic constructs can vary among different Java compiler implementations, which means that .class files can vary among different implementations as well. Consequently, you may have compatibility issues if you serialize an inner class and then deserialize it with a different JRE implementation.
My question(s) are:
- Are there any circumstances under which serialization/deserialization of non-static inner classes can still be succesful, despite the discouragement?
- Are there any workarounds for this? Like alternatives in third party libraries which can tackle this without problem? And if so, why are these able to tackle it, while java cannot?