CopyOnWriteArrayList is marked Serializable. But it’s internal state is transient. Can anyone please answer what are we trying to serialize in this type of List.
/** The array, accessed only via getArray/setArray. */
private volatile transient Object[] array;
The
writeObjectmethod has been overridden to store the state in specific manner. So the actual store, i.e.arraybeing transient is not affecting the serialization of a CopyOnWRiteArrayList object.The transient members in an object will be left during the default serialization process by the JVM. But, if you override
writeObject(), then that method definition will be used for serializing the object instead of the default serialization strategy.