public class ClassTest { private int value = getValue("value"); public ClassTest() { int classValue = getValue("Constructor"); } public static void main(String[] args) { new ClassTest(); } private static int getValue(String who) { System.out.println(who + " called me!"); return 1; } } 输出: value called me! Constructor called me!
int classValue = getValue("Constructor");
} public static void main(String[] args) {
new ClassTest();
} private static int getValue(String who) {
System.out.println(who + " called me!");
return 1;
}
}
输出:
value called me!
Constructor called me!
(2)、绑定构造器,为形参分配内存,并将实参的值赋给形参。构造器还没有调用就为形参分配内存了?方法还没调用没道理在栈上分配内存啊。至于第三点在第四点调用之前调用,这个是好验证的:public class Test {
private int a = init();
private int init(){
System.out.println("init method");
return 88;
}
public Test(){
System.out.println("构造函数");
}
public static void main(String[] args) {
new Test();
}
}
打印结果:
init method
构造函数
深入java虚拟机一书的168页,讲的明明白白的: