通過(guò)代碼實(shí)例解析JAVA類生命周期
代碼
public class TestClass { public static int k=0; public static TestClass t1=new TestClass('t1'); public static TestClass t2=new TestClass('t2'); public static int i=print('i'); public static int n=99; public int j=print('j'); { print('構(gòu)造塊'); } static { print('靜態(tài)塊'); } public TestClass(String str) { System.out.println((++k)+''+str+'i='+i+'n='+n); ++n; ++i; } public static int print(String str){ System.out.println((++k) +':' + str + ' i=' + i + ' n=' + n); ++n; return ++ i; } public static void main(String[] args){ System.out.println('=========start============'); TestClass t = new TestClass('init'); System.out.println('=========end============'); }}
執(zhí)行結(jié)果
1:j i=0 n=02:構(gòu)造塊 i=1 n=13t1i=2n=24:j i=3 n=35:構(gòu)造塊 i=4 n=46t2i=5n=57:i i=6 n=68:靜態(tài)塊 i=7 n=99=========start============9:j i=8 n=10010:構(gòu)造塊 i=9 n=10111initi=10n=102=========end============
講解
代碼由上到下 依次執(zhí)行
由static修飾的變量或方法是在方法區(qū),屬于類變量、類方法
new對(duì)象這個(gè)操作,new出來(lái)的對(duì)象在堆里 對(duì)象的引用在棧里
new出來(lái)的對(duì)象 方法快先構(gòu)造方法執(zhí)行
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. idea設(shè)置提示不區(qū)分大小寫的方法2. 使用AJAX(包含正則表達(dá)式)驗(yàn)證用戶登錄的步驟3. Java PreparedStatement用法詳解4. Java實(shí)現(xiàn)的迷宮游戲5. django queryset相加和篩選教程6. IDEA 2020.1.2 安裝教程附破解教程詳解7. Java利用TCP協(xié)議實(shí)現(xiàn)客戶端與服務(wù)器通信(附通信源碼)8. JS圖片懶加載庫(kù)VueLazyLoad詳解9. 利用ajax+php實(shí)現(xiàn)商品價(jià)格計(jì)算10. Spring如何集成ibatis項(xiàng)目并實(shí)現(xiàn)dao層基類封裝

網(wǎng)公網(wǎng)安備