Unable to understand class object

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:

  1. 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?
+7
java multithreading synchronized class
source share
2 answers

1. When was it created?

It is created when the class is loaded by the JVM using the class loader. A class is loaded when it is referenced by some other class. A ClassLoader usually creates this instance of Class when calling ClassLoader#loadClass(String className) . This is explained in this link from the Java Language specification :

Downloading refers to the process of finding the binary form of a class or interface type with a specific name, possibly by calculating it on the fly, but most often by obtaining the binary representation previously computed from the source code by the Java compiler, and constructing a Class object from this binary form class or interface representations.

2. Does the garbage collect at some point?

Like any other instance, if the Class instance is no longer available, it is entitled to the GC. This happens when the type object represented by the Class instance is unavailable, and the class loader loading the class is also unavailable.

3. How is this used by the synchronous static method, does this mean that there will be only one instance of the class object for the JVM?

Not necessary. If you define a custom class loader, then you can have two instances of Class . In this case, you can even get a ClassCastException if you try to convert an object from some class A to the "same type" A if they were loaded by two different classloaders.

+8
source share

The class does not have an open constructor. Instead, class objects are automatically created by the Java virtual machine as the classes load and the defineClass method is called in the class loader.

  1. As long as class instances are used, and references to the class object are preserved, it will remain in memory.

  2. Yes. The class is immutable, so there is no real synchronization there.

+1
source share

All Articles