Search code examples
javaclassjvmsynchronize

Is the Class object A created when the JVM loads class A, or when I call A.class?


As I know, every class has a Class object. There is one case when I use synchronize, for example:

public class A {
    public static void main(String... args){
        synchronize(A.class){
            //do something...
        }
    }
}

This will lock A's Class object, right? When is this Class object created? Is it created when the JVM loads the A class or when I call A.class? I can't find detail in JLS, could someone please provide the link about it?


Solution

  • this will lock A's Class object, right?

    yes.

    my question is when this Class object is created? is it created at JVM load A class or when i call A.class?

    When the ClassLoader loads it, it returns a Class object.

    i can't find detail in JLS, could someone please provide the link about it, thanks.

    I suggest reading the javadoc for the ClassLoader.loadClass()