Since everything can be deserialized back to java.lang.Object, as each class in java extends java.lang.Object. If you manage to serialize an object that has a non-serializable field, you have no way to find out the class of this field during deserialization. Since each class is an object, you can always return to the Object class.
class NonSerializableUser {} class SerializableUser implements Serializable{} class SomeObject implements Serializable{ public NonSerializableUser nonUser; public SerializableUser user; public Object nonUserObj; public SomeObject(SerializableUser u, NonSerializableUser uu, NonSerializableUser uuu){ user = u; nonUser = uu; nonUserObj = uuu; } }
In this example, deserializing this class will cause nonUser to be empty, and the user to be the correct instance of the SerializableUser class, and nonUserObj to be non-null, but it will lose all methods and NonSerializableClass fields, they will not be serialized. The only parts of this instance that get serialized are the methods and fields that belong to the object.
It is worth noting that many serialization libraries (ObjectOutputStream, for example) will complain about a non-serializable class and will not serialize this object in the first place. This is why I forgot the details of the serialization / deserialization phase. However, many xml frameworks will still serialize these classes, and this is usually the situation where this error occurs in the head.
Jberg source share