作者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