Oracle Java documentation on internal locks and synchronization says:
You may wonder what happens when a static synchronized method is called, since the static method is associated with a class, not an object. In this case, the thread receives an internal lock for the class object associated with the class. Thus, access to the class by static fields is controlled by a lock other than locking for any instance of the class.
I did not quite understand the concept of a Class object . After exploring some online content, I find out:
A class object is a kind of meta object that describes the class of the object, for example, name, package, etc.
My questions:
- When is it created?
- Does garbage collect at some point?
- How is it used by the synchronous static method, does this mean that there will be only one instance of a class object in the JVM?
There is a similar question what is a class object (java.lang.class) in java . But he does not answer my questions.
[Update]
In the comment section of the answer provided by manouti , a new question is added as it mentions that there may be multiple instances of the Class object:
- Is there a chance that a static synchronized method can be accessed by multiple threads simultaneously if there are multiple instances of a class object?
java multithreading synchronized class
Kartic
source share