I have a simple object like this:
public class MyObject
{
private final Integer usefulField;
public MyObject(int parameter){usefulField=parameter)
....
}
It has a field that I want to make sure is there before any other method of this class gets called. I made that field final and asked for it in the constructor. Great.
However now I need to serialize this object. It seems like in this case life would be so much easier if this object was written less safely with a no-args constructor and a setter/getter. Sure there are serialization libraries that sometimes manage to overcome this issue but for the most part they are mysterious and sometimes stop working arbitrarily.
There seems therefore to be a trade-off between how java wants to be written safely and how it wants to be written for serialization. Am I missing a beat here? Is there some way to avoid this issue (besides writing a whole duplicate set of factory classes like I currently do)?