According to this forum, and many other related links. If formatters is null, then the NullPointerException will be thrown.
You get a NullPointerException. For example:
class A {
public static void main(String[] ss) {
Object o = null;
synchronized (o) {
}
}
}
Gives you:
Exception in thread "main" java.lang.NullPointerException
at A.main(A.java:4)
Sssh, you're not supposed to know those are actually references to objects! Think of them as they're presented - like an object - and not how they're implemented. The Object class provides a single lock, so your formatters object will have inherited it. If formatters happens to be null, then synchronizing on it will throw a NullPointerException.
From the specification:
"SynchronizedStatement: synchronized ( Expression ) Block"
... Otherwise, if the value of the Expression is null, a NullPointerException is thrown."
http://java.sun.com/docs/books/jls/third_edition/html/statements.html#14.19
Where possible don't synchronize on objects that are actually used. Synchronize on private final Objects that you create within the class that does the locking. Reason for this is that others might choose the same object to synchronize on, which means you have no idea what kind of side effects this locking has.
It won't work. You synchronize on objects, not on variables. When the variable is null, there is no object to synchronize on, so an Exception is thrown.