Here it says that synchronizing constructors is a syntax error. Then it presents a warning like this and this statement i am unable
to understand. I am copying the statement here.
When constructing an object that will be shared between threads, be very careful that a reference to the object does not "leak" prematurely. For example, suppose you want to maintain a List called instances containing every instance of class. You might be tempted to add the line
to your constructor. But then other threads can use instances to access the object before construction of the object is complete.
What does the last line trying to say. But then other threads can use instances to access the object before construction of the object is
complete? Can someone explain me this?
--Lost in preparation of SCJP and SCWCD--
"Start writing a new chapter, for if you live by the book you'll never make history." (Ben Sobel)
An object is constructed completely only when its constructor completes. If you allow other threads access to the object before the constructor completes, the threads might actually be working with an incomplete instance of the object, which could lead to problems.