Important Notice: Our web hosting provider recently started charging us for additional visits, which was unexpected. In response, we're seeking donations. Depending on the situation, we may explore different monetization options for our Community and Expert Contributors. It's crucial to provide more returns for their expertise and offer more Expert Validated Answers or AI Validated Answers. Learn more about our hosting issue here.

What is the point of creating the temporary reference to this.layoutMgr?

0
Posted

What is the point of creating the temporary reference to this.layoutMgr?

0

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

0

This code is from the 1.0 AWT, and the programmer was probably pretty skilled.

Related Questions

What is your question?

*Sadly, we had to bring back ads too. Hopefully more targeted.

Experts123