How JVM loads parent classes in Java(JVM如何加载Java中的父类)
问题描述
代码:
class A {
static {
System.out.println("loading A static 1");
}
static {
System.out.println("loading A static 2 B.c= "+B.c);
}
static {
System.out.println("loading static 3");
}
static int a=10;
A(){
}
}
class B extends A{
static {
System.out.println("loading B A.a= "+A.a);
}
static int c = 50;
}
public class Test {
public static void main(String[] args) {
new B();
}
}
输出:
loading A static 1
loading A static 2 B.c= 0
loading static 3
loading B A.a= 10
从这里我们可以说父类在子类之后加载,而子类在父类之后初始化吗?如果是这样,JVM如何加载类层次结构?
From this out put can we say that the parent class loads after the child class but child class initialize after the parent class? If so how JVM loads class hierarchies?
推荐答案
您可以通过执行 java -verbose Test
来检查:
You can check this by executing it with java -verbose Test
:
...
[Loaded A from file:/.../src/main/java/]
[Loaded B from file:/.../src/main/java/]
loading A static 1
loading A static 2 B.c= 0
loading static 3
loading B A.a= 10
...
所以不,父类也是先加载的.
So no, the parent class is loaded first too.
这篇关于JVM如何加载Java中的父类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:JVM如何加载Java中的父类


- 从 finally 块返回时 Java 的奇怪行为 2022-01-01
- 如何使用WebFilter实现授权头检查 2022-01-01
- Java包名称中单词分隔符的约定是什么? 2022-01-01
- value & 是什么意思?0xff 在 Java 中做什么? 2022-01-01
- Jersey REST 客户端:发布多部分数据 2022-01-01
- C++ 和 Java 进程之间的共享内存 2022-01-01
- Spring Boot连接到使用仲裁器运行的MongoDB副本集 2022-01-01
- Eclipse 插件更新错误日志在哪里? 2022-01-01
- 将log4j 1.2配置转换为log4j 2配置 2022-01-01
- Safepoint+stats 日志,输出 JDK12 中没有 vmop 操作 2022-01-01