What is the point of creating the temporary reference to this.layoutMgr?
Ans : This code is from the 1.0 AWT, and the programmer was probably pretty skilled. public synchronized void layout() { LayoutManager layoutMgr = this.layoutMgr; if (layoutMgr != null) { layoutMgr.layoutContainer(this); } } The code makes a local copy of a global variable for one or both of two reasons. The first reason is that accessing local variables can be faster than accessing (non final) member variables. It’s good for loops or where there are many references in the source. The second reason is so that even if other threads update the global, this.layoutMgr = someOtherLayoutMgr; This method will still have a pointer to the original layoutMgr. If the local variable were omitted, and another thread used the setLayout() method to change layoutMgr to null between when the layout method checked for null and when it invoked layoutMgr’s layoutContainer method, a NullPointerException would result. Note that the synchronized keyword on the layout method doesn’t help any, since setLayout