This question is about ObjectInputStream and how it builds fields declared as transient. Considering a simple use-case of ObjectInputStream
FileInputStream fis = new FileInputStream("t.tmp");
ObjectInputStream ois = new ObjectInputStream(fis);
SomeClass sc = (SomeClass) ois.readObject();
Where SomeClass is
class SomeClass {
int x;
transient OtherClass y;
}
class OtherClass {
int z;
}
what will be the value of sc.y after ois.readObject ?
I am asking to clarify what i read at docs.oracle.com which states
“Fields declared as transient or static are ignored by the deserialization process. References to other objects cause those objects to be read from the stream as necessary.”
What does it mean that transient fields are ignored? And how can they be read from the stream if they are transient (ie not serialized – how i understand it…)
Matthias
Someclass.ywill be the “default value”. In this case, since it’s an object, it will benull.They’re not serialised – they’re skipped. From the Java Object Serialisation Specification:
… and …
And your next question:
They’re not in the stream – so, actually, they’re not read. As mentioned above, the end up as the ‘default value’ for that type (
nullfor objects,0/falsefor primitives).For the most common case, they’re ignored by the serialisation process – and because of that, they’re not in the stream, and won’t be deserialised. However, if you changed the class subsequent to serialising the object so that a field that used to be serialised is now marked as transient, then the deserialisation process will ignore that value when it finds it in the stream.