I'm looking to save the state of a complex swing o...
# serialization
j
I'm looking to save the state of a complex swing object to a file and load it. Is serialization a good tool for this? I don't want to serialize the whole thing. Rather pick a few properties to serialize and use them to initialize the instance with.
a
Obviously if you don't save all the properties, you cannot reconstruct the exact object. However many class instances have some kind of temporary state which is fine to be discarded. In that case you can simply annotate those properties with
@Transient
and supply a default initialization value, those properties won't be serialized and upon deserialization, the default value will be used. For more complex use-cases, you can create factory methods which can accept a subset of properties and create a full object, perhaps with some logic to determine values for the missing properties. Another option is custom serializers which probably offers the most customization but is also the most complex to create.
j
Thanks for the input Andy.
I'd be interesting in also trying the factory object approach. I currently have a surrogate-class implementation working but it's still quite complex to do correctly with swing objects. Also, the surrogate class doesn't have visibility to the private members of the original swing subclass so, feels like I'm still against the current.