Class 对象A 是在JVM 加载class A 时创建的,还是在我调用A.class 时创建的?
Is the Class object A created when the JVM loads class A, or when I call A.class?
据我所知,每个 class 都有一个 Class
对象。我使用synchronize时有一种情况,例如:
public class A {
public static void main(String... args){
synchronize(A.class){
//do something...
}
}
}
这将锁定 A
的 Class
对象,对吗?这个 Class
对象是什么时候创建的?它是在 JVM 加载 A
class 时创建的还是在我调用 A.class
时创建的?我在 JLS 中找不到详细信息,有人可以提供 link 吗?
它是在 class 被 JVM 加载为 Javadocs 状态时创建的:
Class
has no public constructor. Instead Class
objects are constructed automatically by the Java Virtual Machine as classes are loaded and by calls to the defineClass
method in the class loader.
this will lock A's Class object, right?
是的。
my question is when this Class object is created? is it created at JVM load A class or when i call A.class?
当 ClassLoader 加载它时,它 returns 一个 Class
对象。
i can't find detail in JLS, could someone please provide the link about it, thanks.
我建议阅读 ClassLoader.loadClass()
的 javadoc
A class 由 ClassLoader when the class is first used, JLS 5.3:
初始化
Creation of a class or interface C denoted by the name N consists of
the construction in the method area of the Java Virtual Machine
(§2.5.4) of an implementation-specific internal representation of C.
Class or interface creation is triggered by another class or interface D, which references C through its run-time constant pool.
Class or interface creation may also be triggered by D invoking
methods in certain Java SE platform class libraries (§2.12) such as
reflection.
Is it created when the JVM loads the A class
是的。
or when I call A.class?
没有。 JVM 在加载使用它的 class 时加载 A,除了反射的特殊情况,此处不适用。
据我所知,每个 class 都有一个 Class
对象。我使用synchronize时有一种情况,例如:
public class A {
public static void main(String... args){
synchronize(A.class){
//do something...
}
}
}
这将锁定 A
的 Class
对象,对吗?这个 Class
对象是什么时候创建的?它是在 JVM 加载 A
class 时创建的还是在我调用 A.class
时创建的?我在 JLS 中找不到详细信息,有人可以提供 link 吗?
它是在 class 被 JVM 加载为 Javadocs 状态时创建的:
Class
has no public constructor. InsteadClass
objects are constructed automatically by the Java Virtual Machine as classes are loaded and by calls to thedefineClass
method in the class loader.
this will lock A's Class object, right?
是的。
my question is when this Class object is created? is it created at JVM load A class or when i call A.class?
当 ClassLoader 加载它时,它 returns 一个 Class
对象。
i can't find detail in JLS, could someone please provide the link about it, thanks.
我建议阅读 ClassLoader.loadClass()
的 javadocA class 由 ClassLoader when the class is first used, JLS 5.3:
初始化Creation of a class or interface C denoted by the name N consists of the construction in the method area of the Java Virtual Machine (§2.5.4) of an implementation-specific internal representation of C. Class or interface creation is triggered by another class or interface D, which references C through its run-time constant pool. Class or interface creation may also be triggered by D invoking methods in certain Java SE platform class libraries (§2.12) such as reflection.
Is it created when the JVM loads the A class
是的。
or when I call A.class?
没有。 JVM 在加载使用它的 class 时加载 A,除了反射的特殊情况,此处不适用。