public E removeLast() {
final Node<E> l = last;
if (l == null)
throw new NoSuchElementException();
return unlinkLast(l);
}
last is a member variable. Why not use it directly in the method, but assign it to a final local variable?
Looked at the code
last is transient, right? If you assign it to a local final variable, you don’t need to check the value every time you use last, right?
Part of the reason is to ensure thread safety. Assuming that this method does not use the l variable but directly references the last member, then the judgment becomes
if(this.last == null)
. If last is assigned to null immediately after passing the judgment, then the next sentenceunlinkLast(this.last)
will have an unknown result.