0

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)?

CarrKnight
  • 2,768
  • 2
  • 23
  • 25
  • to be fair this is not just a java question. The same tradeoff occurs for example in dart https://www.dartlang.org/articles/serialization/ ; I feel this is a oo question with a java example so I am not sure the intricacies of the JVM really answer it – CarrKnight May 03 '16 at 10:14
  • I sympathize with your point, but I don't see how this question can be usefully answered without considering the intricacies of a specific language. In that case the question would probably be closed again as too broad. – tjd May 03 '16 at 13:40
  • would a move to the programmer stackexchange be a decent compromise, perhaps? Otherwise I can just proceed to delete this, thank you. – CarrKnight May 03 '16 at 13:42

0 Answers0