您好,登錄后才能下訂單哦!
代碼
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)容,希望對大家的學習有所幫助,也希望大家多多支持億速云。
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。