Oracle Java documentation on Intrinsic Locks and Synchronization says:
You might wonder what happens when a static synchronized method is invoked, since a static method is associated with a class, not an object. In this case, the thread acquires the intrinsic lock for the Class object associated with the class. Thus access to class's static fields is controlled by a lock that's distinct from the lock for any instance of the class.
I did not completely understand the concept of Class object
. After studding some online content I get to know:
A Class object is sort of a meta object describing the class of an object such as name, package etc.
My questions are:
- When is it created?
- Is it Garbage Collected at some point of time?
- As it is used by synchronized static method, does it mean there will be only one instance of Class object per JVM?
There is a similar question what is Class Object(java.lang.class) in java. But it doesn't answer my questions.
[Update]
A new question is added in the comment section of the answer provided by manouti
as he mentioned there can be multiple instance of Class
object:
- Is there any chance that a static synchronized method can be accessed by multiple thread simultaneously if multiple instance of Class object exists?