Mike said:
Lee Fesperman said:
They're synchronized on the associated 'Class' object ... equivalent to:
static void method()
{
synchronized (ClassName.class)
{
...
}
}
Someone pointed out to me once that there's nothing in the Java docs that
says explicitly "All calls to ClassName.class" will result in the same
object" [1], but the fact that static synchronization works the way it does
implies this.
Yep, it seems that the Class object for a given class should be 'unique'. The javadocs
I'm looking at sorta imply that this is true by explicitly saying that Class objects for
each type of array are unique -- "Every array also belongs to a class that is reflected
as a Class object that is *shared* by all arrays with the same element type and number
of dimensions." (emphasis mine). The javadocs for Class.forName() also say "Returns the
Class object associated with the class or interface with the given string name.", rather
than saying "Returns a Class object".
I also think it should be unique within a given classloader but not between
classloaders, lest unexpected (and unfortunate) conflicts arise.
Synchronization of static methods provide another distinction from instance methods.
Synchronized instance methods in super/sub classes all synchronize on the same monitor
within an instance. A synchronized static method synchronizes on a different monitor
than a synchronized static method in its superclass.
1. It isn't, as far as I know, guaranteed that all calls to
Class.getMethod(), for instance, result in the same Method object.
As Thomas mentioned, Method objects are mutable ... because of setAccessible().