Simple question:
Why would this be preferred:
public class Foo {
final private static Object foo = new Object();
public static void doSomething() {
synchronized(Foo.foo) {
//code
}
}
}
over this:
public class Foo {
public static void doSomething() {
synchronized(Foo.class) {
//code
}
}
}
or this:
public class Foo {
public synchronized static void doSomething() {
//code
}
}
?
To me these all look essentially identical, so I'm not sure what would be the best way to synchronize access to static fields, or why one would be better than another, but I've heard the first is often preferred.