Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 265
Tony Avatar asked Sep 28 '15 09:09

Tony


1 Answers

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()

like image 144
Peter Lawrey Avatar answered Sep 22 '22 21:09

Peter Lawrey