標籤:對象 變數 靜態 stat name 調用 static public void
1.因為new Studen()用到了Student類,所以會把它從硬碟上載入進入記憶體
2.如果有static靜態代碼塊就會隨著類的載入而執行,還有靜態成員方法和普通方法也會隨著類的載入而被載入
3.在堆中開闢空間,分配記憶體位址
4.在堆中建立對象特有屬性(非靜態成員),並同時對特有屬性進行預設初始化
5.對屬性進行顯示初始化 比如 在類中 String name="123";
6.執行構造代碼塊,對所有對象進行初始化
7.執行對應的建構函式,對對象進行初始化(調用不同的建構函式)
8.講記憶體位址給棧中的變數
執行個體如下
package j2se;/* *靜態代碼塊隨著類的載入而執行,優先於主函數執行,切只執行一次 *new 對象時,先調用構造代碼塊,再執行相應的構造方法 */class Student{String name="fff";int age;private static String country="中國";public Student(){System.out.println("我是無參構造方法");}public Student(String name){this.name=name;System.out.println("我是一個參數構造方法");}public Student(String name,int age){this(name);this.age=age;System.out.println("我是兩個參數構造方法");}public static void get(){System.out.println("我是靜態成員方法");}static{System.out.println("我是靜態代碼塊");}{System.out.println("我是構造代碼塊");}}public class A6_43 {public static void main(String[] args) {Student a=new Student();Student b=new Student("小明");Student c=new Student("小明",27);}}
運行結果:
我是靜態代碼塊
我是構造代碼塊
我是無參構造方法
我是構造代碼塊
我是一個參數構造方法
我是構造代碼塊
我是一個參數構造方法
我是兩個參數構造方法
java基礎_對象初始化過程