If I have a class similar to the one below, and have a synchronized block for the object level lock on "_object", will that prevent the getStaticObject() method from returning the "_object", which may be null, if the reinitObject() method is called?
It seems like both methods should be static and synchronized, and the synchronized block should synch on the class level lock... to prevent the getStaticObject() method from returning a null, right?
Which is the best approach to preventing getStaticObject() from ever returning null?
Also, most of us find it difficult to read posts in which the code has no formatting. Please use [code] tags to get the formatting to display so it's readable. You can edit your previous post using the little pencil-and-paper icon at the top of the post.
To safeguard a static variable, you need to synchronize on the class rather than a particular object. Otherwise, different threads could obtain object locks of different instances and access the (same) static member in an "unsynchronized" manner.
Because your methods are already static, you only need the "synchronized" keyword before the return type -- you do not need a separate block synchronized explicitly on the class.
"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer sscce.org
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus