系统 class 的 out 对象何时在 Java 中初始化?
When is the out object of the System class initialized in Java?
修改为final
的变量必须在声明或执行构造函数时初始化。
我在系统Class文件中查找,发现out
对象是在private static void initializeSystemClass()
方法中初始化的,这个方法是什么时候调用的?
private static native void registerNatives();
static { registerNatives();
}
在System.java文件中,我们可以在标题行看到这段代码。调用registerNatives方法,让VM调用initializeSystemClass方法初始化System Class.
根据 System
class 中的注释,此方法由 JVM
调用。在下面添加了部分评论:
public final class System {
/* register the natives via the static initializer.
*
* VM will invoke the initializeSystemClass method to complete
* the initialization for this class separated from clinit.
* Note that to use properties set by the VM, see the constraints
* described in the initializeSystemClass method.
*/
private static native void registerNatives();
static {
registerNatives();
}
/** Don't let anyone instantiate this class */
private System() {
}
修改为final
的变量必须在声明或执行构造函数时初始化。
我在系统Class文件中查找,发现out
对象是在private static void initializeSystemClass()
方法中初始化的,这个方法是什么时候调用的?
private static native void registerNatives();
static { registerNatives();
}
在System.java文件中,我们可以在标题行看到这段代码。调用registerNatives方法,让VM调用initializeSystemClass方法初始化System Class.
根据 System
class 中的注释,此方法由 JVM
调用。在下面添加了部分评论:
public final class System {
/* register the natives via the static initializer.
*
* VM will invoke the initializeSystemClass method to complete
* the initialization for this class separated from clinit.
* Note that to use properties set by the VM, see the constraints
* described in the initializeSystemClass method.
*/
private static native void registerNatives();
static {
registerNatives();
}
/** Don't let anyone instantiate this class */
private System() {
}