The deserialization instance method readObject
is private. There is no way to call it from the outside. You could call it from one of your instance methods, but that would be very strange and I'd question why you'd be doing that in the first place. You say:
No part of an object's state should have any bearing on the process of constructing another object from data.
True, but I don't see why you think this would be an issue. There's no way you could call readObject
from the outside (unless you call it from some other public method, which as I said, is kind of iffy) on an instance that you have already created. When you deserialize, you will most probably be using ObjectInputStream
, which will use the no-args constructor to create a new instance, and then will hydrate that object using the data from the stream (when you call ObjectInputStream#readObject
). So there is no question of the state of the instance affecting deserialization, because what you get back is an instance created from the serialized data (as Object
, but you will then cast it to the concrete type).
In effect, readObject
behaves somewhat like a constructor, except that it uses previously-serialized data to create an instance of an object. Extending the analogy, your question wouldn't make sense because you would be asking "Why does creating an object using the constructor have anything to do with the state of the instance?". The question of state doesn't even apply because you don't even have an instance! Similarly, state doesn't come into play with readObject
because can never* deserialize and create an instance by using an existing instance.
If you want greater control over serialization, you can override readObject
and writeObject
from Serializable
within your class if you want to handle things in a special way. You can exert greater control over how the data is written out by implementing Externizable
and providing implementations for readExternal
and writeExternal
.
In your second question you're wondering what the "something" is that calls readObject
. The "something" is reflection; ObjectInputStream
will check to see if the class has a readObject
method. If you've provided your own implementation, it will call that. Otherwise it will call defaultReadObject
(which contains the logic for default serialization).
As far as built-in factories for deserialization, there isn't anything and I haven't really felt a need something since the standard serialization/deserialization approach seems to work well.
If you want more information on this, I suggest taking a look at the serialization specification for a comprehensive and in-depth view of how Java tackles serialization, and specifically Object Input Classes for your particular question.
*The only way state comes into it is if you do something strange like calling the readObject
method from some other instance method (which would have to take in an ObjectInputStream
), and then you have custom logic that performs deserialization based on the state of the existing instance. In other words, the only way the object's state has any bearing on deserialization logic is if you explicitly write it that way. Again, as I mentioned before, that would be very strange code, with a whole lot of caveats and of minimal value.