java基礎_對象初始化過程

來源:互聯網
上載者:User

標籤:對象   變數   靜態   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基礎_對象初始化過程

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.