我读到了我在下面提到的对象块:
class Check {
static {
System.out.println("hello from Static");
}
//object block
{
System.out.println("This is my object block");
}
public static void main(String args[]) {
}
}
这是程序的输出
Hello from Static
但是当我制作类的对象时,就会执行对象块。我不明白这个过程是如何由 jvm 完成的。垃圾收集器有什么作用吗?
class Check {
static {
System.out.println("hello from Static");
}
//object block
{
System.out.println("This is my object block");
}
public static void main(String args[]) {
System.out.println("hello from main");
Check obj = new check();
}
}
这是输出:
hello from Static
hello from main
This is my object block