作者Oreilly (怕寂寞卻愛自由)
看板java
標題Re: [問題] 請教物件生成的流程
時間Sun Mar 5 12:19:43 2006
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
;asm code generated for getInstance
054D20B0 mov eax,[049388C8] ;load instance ref
054D20B5 test eax,eax ;test for null
054D20B7 jne 054D20D7
054D20B9 mov eax,14C0988h
054D20BE call 503EF8F0 ;allocate memory
054D20C3 mov [049388C8],eax ;store pointer in
;instance ref. instance
;non-null and ctor
;has not run
054D20C8 mov ecx,dword ptr [eax]
054D20CA mov dword ptr [ecx],1 ;inline ctor - inUse=true;
054D20D0 mov dword ptr [ecx+4],5 ;inline ctor - val=5;
054D20D7 mov ebx,dword ptr ds:[49388C8h]
054D20DD jmp 054D20B0
這篇文章推論出
step3和step4順序應該倒過來
這在多執行緒的情況下是合理的
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
所以小弟我只想知道單一執行緒底下且不考慮繼承
有沒有人能證明物件生成的流程
感謝ing...
--
dd
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 61.221.67.175