After looking the source code of some Java Collection classes, I found that the member variables are always being modified by using transient
.
For instance, the LinkedList
source code:
public class LinkedList<E> extends AbstractSequentialList<E> implements List<E>, Deque<E>, Cloneable, java.io.Serializable
{
private transient Entry<E> header = new Entry<E>(null, null, null);
private transient int size = 0;
public LinkedList()
{
header.next = header.previous = header;
}
public LinkedList(Collection<? extends E> c) {
this();
addAll(c);
}
// ...other stuff
}
Of course, not only LinkedList
uses transient, almost every Java collection classes use transient
to modify at least half of their member variables.
So, my question is: why transient
used so widely in the Java standard library?
(Of course everyone knows the definition and usage of transient
, but that's not my question:)