作者tkcn (小安)
看板java
标题Re: [问题] 请教物件生成的流程
时间Sun Mar 5 12:47:59 2006
※ 引述《Oreilly (怕寂寞却爱自由)》之铭言:
: MyClass my=new MyClass();
: 这行code做了以下事情
: step1.宣告 MyClass型态的变数my
: step2.在heap上建立MyClass 物件
: step3.执行MyClass的建构子
: step4.把记忆体的位置 asign reference给变数my
: ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
: 可是这篇文章
: http://www-128.ibm.com/developerworks/java/library/j-dcl.html
: Listing 6. Assembly code produced from code in Listing 5
请注意看最後一段
"Not all JIT compilers generate the code as above."
---
public class Test {
static Test a;
public Test(){
try{
Thread.sleep(3000);
}
catch(InterruptedException e){}
System.out.println("Constructor finish!!");
}
public static void main (String [] args){
Thread t =
new Thread(
new Runnable(){
public void run(){
for(
int i=0;i<10;i++){
System.out.println(a);
try{
Thread.sleep(500);
}
catch(InterruptedException e){}
}
}
});
t.start();
a=
new Test();
}
}
---
执行结果:
null
null
null
null
null
null
null
Constructor finish!!
Test@757aef
Test@757aef
Test@757aef
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 220.131.67.94