Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java - why transient member variables used so widely in Java standard library?

Tags:

java

transient

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:)

like image 785
Fred Pym Avatar asked Nov 11 '15 09:11

Fred Pym


People also ask

Why do we use transient variables?

transient is a variables modifier used in serialization. At the time of serialization, if we don't want to save value of a particular variable in a file, then we use transient keyword. When JVM comes across transient keyword, it ignores original value of the variable and save default value of that variable data type.

What is transient variable in Java?

A transient variable is a special type of variable which we create by using the transient keyword. It is a special type of variable which have a non-serialized value at the time of serialization. A variable that is initialized by its default value during de-serialization is known as a transient variable.

What is the use of the transient keyword?

The transient keyword in Java is used to avoid serialization. If any object of a data structure is defined as a transient , then it will not be serialized. Serialization is the ​process of converting an object into a byte stream.


1 Answers

From serialization point of view transient variables will not be serialized when serializing the whole object.

When you don't want some variable to be serialized you make it transient

From your example LinkedList is serializable. If you look carefully all the variable that are made transient are maintained programmatically. SO there is no need to persist them.

For example size, when you are reading back any serialized object you are reading just Node<E> and maintaining the size programmatically. So there is no need to serialize the size. Remember the real data of a LinkedList is not its size. If you have the real data which are the entries you can calculate its size any time and its easier this way.

For Reference please have a look.

@SuppressWarnings("unchecked")
private void readObject(java.io.ObjectInputStream s)
    throws java.io.IOException, ClassNotFoundException {
    // Read in any hidden serialization magic
    s.defaultReadObject();

    // Read in size
    int size = s.readInt();

    // Read in all elements in the proper order.
    for (int i = 0; i < size; i++)
        linkLast((E)s.readObject());
}

void linkLast(E e) {
    final Node<E> l = last;
    final Node<E> newNode = new Node<>(l, e, null);
    last = newNode;
    if (l == null)
        first = newNode;
    else
        l.next = newNode;
    size++;
    modCount++;
}
like image 142
Saif Avatar answered Sep 17 '22 20:09

Saif