通過代碼實例解析JAVA類生命周期
更新時間:2020年08月28日 11:18:33 作者:古城,老巷
這篇文章主要介紹了通過代碼實例解析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("構造塊");
}
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í)行結果
1:j i=0 n=0 2:構造塊 i=1 n=1 3t1i=2n=2 4:j i=3 n=3 5:構造塊 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:構造塊 i=9 n=101 11initi=10n=102 =========end============
講解
代碼由上到下 依次執(zhí)行
由static修飾的變量或方法是在方法區(qū),屬于類變量、類方法
new對象這個操作,new出來的對象在堆里 對象的引用在棧里
new出來的對象 方法快先構造方法執(zhí)行
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
使用IDEA直接連接MySQL數(shù)據(jù)庫的方法
這篇文章主要介紹了如何使用IDEA直接連接MySQL數(shù)據(jù)庫,首先需要新建一個空項目,第一次連接 需要先下載驅動,文中給大家介紹的非常詳細,感興趣的朋友跟隨小編一起看看吧2024-04-04
Java8中用foreach循環(huán)獲取對象的index下標詳解
這篇文章主要給大家介紹了關于Java8中用foreach循環(huán)獲取對象的index下標的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-04-04
mybatis-plus查詢無數(shù)據(jù)問題及解決
這篇文章主要介紹了mybatis-plus查詢無數(shù)據(jù)問題及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-12-12

