通過代碼實例解析JAVA類生命周期
更新時間:2020年08月28日 11:18:33 作者:古城,老巷
這篇文章主要介紹了通過代碼實例解析JAVA類生命周期,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
代碼
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=0 2:構(gòu)造塊 i=1 n=1 3t1i=2n=2 4:j i=3 n=3 5:構(gòu)造塊 i=4 n=4 6t2i=5n=5 7:i i=6 n=6 8:靜態(tài)塊 i=7 n=99 =========start============ 9:j i=8 n=100 10:構(gòu)造塊 i=9 n=101 11initi=10n=102 =========end============
講解
代碼由上到下 依次執(zhí)行
由static修飾的變量或方法是在方法區(qū),屬于類變量、類方法
new對象這個操作,new出來的對象在堆里 對象的引用在棧里
new出來的對象 方法快先構(gòu)造方法執(zhí)行
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot 整合 dubbo xml實現(xiàn)代碼示例
這篇文章主要介紹了SpringBoot 整合 dubbo xml實現(xiàn)代碼示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-03-03SpringBoot分頁的實現(xiàn)與long型id精度丟失問題的解決方案介紹
在以后的開發(fā)中,當(dāng)全局唯一id的生成策略生成很長的Long型數(shù)值id之后會超過JS對Long型數(shù)據(jù)處理的能力范圍,可能發(fā)生精度丟失而造成后端方法失效,我們要學(xué)會解決。分頁功能雖然簡單但是非常重要,對于剛接觸項目的人一定要重點注意2022-10-10Java POI實現(xiàn)將導(dǎo)入Excel文件的示例代碼
這篇文章主要介紹了Java POI實現(xiàn)將導(dǎo)入Excel文件的示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-02-02使用springmvc參數(shù)接收boolean類型參數(shù)的問題
這篇文章主要介紹了使用springmvc參數(shù)接收boolean類型參數(shù)的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-01-01