I was reading the source of Java\'s ArrayList and I came across its backing array declaration:
private transient Object[] elementData;
Why
Why does this need to be transient?
It does this because it provides custom readObject and writeObject methods that do a better job of serialization than the default. Specifically, the writeObject method writes just the size and the sequence of elements. This avoids serializing the private array object which 1) has its own header and overheads, and 2) is typically padded with nulls. The space saving can be significant.
Why can't this class be serialized?
The ArrayList class as a whole can be serialized1. The Object[] could be serialized directly, but they chose to mark it as transient implement the serialization another way.
1 - Actually, this depends on the elements' runtime types. For example, if you attempted to serialize an ArrayList containing Thread references, then you would get a runtime exception for the first non-null reference.