乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      java 初始化總結

       流浪的星星318 2017-02-15

      相信好多人對Java初始化問題一直存有疑惑,下面是我看到的比較詳細的java初始化問題講解

      一  java初始化基礎知識
      1、 一個類的所有基本類型數(shù)據(jù)成員都會保證獲得一個初始值。 
      非基本類型,會初始化為null 

      Java代碼 
      1.   public class Initialization {  
      2. int a;  
      3. char b;  
      4. short s;  
      5. float f;  
      6. long lo;  
      7. double dou;  
      8. byte e;  
      9. boolean flag;  
      10.        Object obj;  
      11.   
      12. public static void main(String [] args){  
      13.     Initialization init = new Initialization();  
      14.     init.print();  
      15. }  
      16.   
      17. public void print(){  
      18.     System.out.println("int a="+a+"/nchar b="+b+" /n"+" short s="+s+"/n float f="+f+"/n long lo="+lo+"/n double dou="+dou+"/n byte e="+e+"/n boolean flag="+flag+"/n Object obj="+obj);  
      19. }  


      出來結果為 

      Java代碼 
      1. int a=0  
      2. char b=  
      3. short s=0  
      4. float f=0.0  
      5. long lo=0  
      6. double dou=0.0  
      7. byte e=0  
      8. boolean flag=false  
      9. Object obj=null  


      可見,java會為類的基本類型的變量提供一個初始值,各類型初始值不同,非基本類型初始為null。注意,這里的變量必須是類變量,注意,只會為類變量提供初始化,而局部變量不會。如果局部變量沒有初始化,會收到一個出錯信息 

       


      2、可以通過構造方法或其他方法進行初始化,但是不會妨礙java默認的初始化

        看下面的例子 

       
      Java代碼 
      1.        int i;  
      2.        Object obj;  
      3. public Initialization(){  
      4.     System.out.println("before i="+i+"  obj="+obj);  
      5.     i = 1;  
      6.     obj = new Object();  
      7.     System.out.println("after i="+i+" obj="+obj);  
      8. }  
      9.   
      10. public static void main(String [] args){  
      11.     Initialization init = new Initialization();  
      12. }  
      13.    


        輸出結果為 

       
      Java代碼 
      1. before i=0  obj=null  
      2. after i=1 obj=java.lang.Object@de6ced  


      由此可見,不論是基本類型,還是其他的類。java默認的初始化是最先發(fā)生的,位于一切方法之前。 
      3、static 數(shù)據(jù)的初始化 
        static 數(shù)據(jù)會發(fā)生上述同樣的事情(基本類型,獲得對應基本類型的初始化值;非基本類型,初始化為null) 
        但是,由于static值只有一個存儲區(qū)域,所以static值只會被初始化一次,看下面的例子 

       
      Java代碼 
      1.     public static void main(String [] args){  
      2. Cupboard cup = new Cupboard();  
      3. cup = new Cupboard();  
      4.     }  
      5.      
      6.     public class Cupboard {  
      7. static Bowl bowl = new Bowl();  
      8.   
      9. public Cupboard(){  
      10.     System.out.println("initialization Cupboard");  
      11. }  
      12.     }  
      13.     public class Bowl {  
      14. public Bowl(){  
      15.     System.out.println("init ing Bowl~");  
      16. }  
      17.     }  
      18.    


        輸出結果如下 

       
      Java代碼 
      1. init ing Bowl~  
      2. initialization Cupboard  
      3. initialization Cupboard  


      所以說,static數(shù)據(jù)只會在第一次進行初始化,之后就不會了。 

      4、初始化順序 
        在一個類中,無論變量的定義是在方法之前還是方法之后,都會在方法之前進行初始化的; 
        另外,static數(shù)據(jù)初始化位于非static數(shù)據(jù)初始化之前 
      來看下邊的例子 

       
      Java代碼 
      1.   public static void main(String [] args){  
      2. Cupboard cup = new Cupboard();  
      3.   
      4.   }  
      5.   public class Cupboard {  
      6. Pan pan = new Pan();  
      7. public Cupboard(){  
      8.     System.out.println("initialization Cupboard");  
      9. }  
      10. static Bowl bowl = new Bowl();  
      11.   }  
      12.   public class Bowl {  
      13. public Bowl(){  
      14.     System.out.println("init ing Bowl~");  
      15. }  
      16.   }  
      17.   public class Pan {  
      18. public Pan(){  
      19.     System.out.println("initialization Pan");  
      20. }  
      21.   }  
      22.    


      結果如下 

      Java代碼 
      1.     init ing Bowl~  
      2. initialization Pan  
      3. initialization Cupboard  
      4.    



      5、靜態(tài)塊 
        靜態(tài)塊里的變量初始化順序位于普通變量之前,和static變量相比,則是完全由定義的順序來決定了。另外,靜態(tài)塊里的變量也是只初始化一次,這點和static變量一致。示例如下 

       
      Java代碼 
      1.    public class Other {  
      2. static{  
      3.     Bowl bowl2 = new Bowl(2);  
      4. }  
      5. static Bowl bowl = new Bowl(1);  
      6.   
      7. private Bowl bowl3 = new Bowl(3);  
      8.   
      9. public static void main(String [] args){  
      10.     Other other = new Other();  
      11.     other = new Other();  
      12. }  
      13.     }  
      14.     public class Bowl {  
      15. public Bowl(){  
      16.     System.out.println("init ing Bowl~");  
      17. }  
      18.   
      19. public Bowl(int i){  
      20.     System.out.println("init ing Bowl"+i);  
      21. }  
      22.     }  
      23.    


      輸出結果為 

        
      Java代碼 
      1. init ing Bowl2  
      2. init ing Bowl1  
      3. init ing Bowl3  
      4. init ing Bowl3  


      如果調換static變量和靜態(tài)塊的位置,輸出結果如下 

       
      Java代碼 
      1. init ing Bowl1  
      2. init ing Bowl2  
      3. init ing Bowl3  
      4. init ing Bowl3  


      6、涉及到繼承時 初始化順序 
        初始化時,如果有static變量或靜態(tài)塊,其初始化順序是位于最前面的,無論變量位于子類還是父類中,它們二者之間的順序,可參見第5點; 
        static變量初始完了后,先初始化父類,然后是子類。 
        示例如下 

       
      Java代碼 
      1.     public class Base {  
      2. Bowl bowl = new Bowl(1);  
      3. public Base(){  
      4.     System.out.println("initialization Class Base");  
      5. }  
      6. static Bowl bowl5 = new Bowl(5);  
      7. static{  
      8.     Bowl bowl6 = new Bowl(6);  
      9. }  
      10.     }  
      11.     public class Sub extends Base {  
      12. Bowl bow2 = new Bowl(2);  
      13. public Sub(){  
      14.     System.out.println("initialize Sub");  
      15. }  
      16. static Bowl bowl3 = new Bowl(3);  
      17.   
      18. static{  
      19.     Bowl bowl4 = new Bowl(4);  
      20. }  
      21.     }  
      22.     public class Test {  
      23. public static void main(String []args){  
      24.     Sub sub = new Sub();  
      25. }  
      26.     }  
      27.    


      輸出結果如下 

       
      Java代碼 
      1. init ing Bowl5  
      2. init ing Bowl6  
      3. init ing Bowl3  
      4. init ing Bowl4  
      5. init ing Bowl1  
      6. initialization Class Base  
      7. init ing Bowl2  
      8. initialize Sub  

      二 問題舉例

       

      package test;

       

       

      class Singleton {  

      private static Singleton obj = new Singleton();   

       

      public static int counter1;  

      public static int counter2 = 0;  

       

      private Singleton() {     

      counter1++;    

      counter2++;     

      }  

       

      public static Singleton getInstance() {  

       

      return obj;  

       

      }    

      }  

      public class MyMain {  

       

      public static void main(String[] args) {  

       

      Singleton obj = Singleton.getInstance();  

       

      System.out.println("obj.counter1=="+obj.counter1);  

       

      System.out.println("obj.counter2=="+obj.counter2);  

       

      }  

       

      }  

       

       

       

       

      這段程序代碼輸出,實際運行結果:

       

       

      obj.counter1==1

      obj.counter2==0

       

       

      相信大家跟我一樣會對這個結果存有疑問,這段代碼中尤其注意:private static Singleton obj = new Singleton();   在類Singleton中的位置,改變位置會有不同結果。關于這段代碼運行結果的解釋:

      當程序執(zhí)行private static Singleton obj = new Singleton(); 句的時候就去調用了Singleton構造器,此時counter1、counter2都是1,但是接著執(zhí)行向下執(zhí)行:public static int counter1;時將1賦給counter1,執(zhí)行public static int counter2 = 0;時重新給counter2賦值為0

       

      三  典型初始化例子

       

      Java初始話很好的一個例子, 摘自Think in Java

       

      Java代碼 
      1. package cn.edu.xupt.test;  
      2.   
      3. //: initialization/StaticInitialization.java  
      4. //Specifying initial values in a class definition.  
      5.   
      6. //無論創(chuàng)建多少對象, 靜態(tài)數(shù)據(jù)都只占用一份存儲區(qū)域  
      7. //初始化的順序是先靜態(tài)對象(如果它們尚未因前面的對象創(chuàng)建過程而被初始化), 而后是"非靜態(tài)"對象  
      8. //載入.class文件(這將創(chuàng)建Class對象),有關靜態(tài)初始化的所有動作執(zhí)行.  
      9. //靜態(tài)初始化只在Class對象首次加載的時候進行一次  
      10. class Bowl {  
      11.     Bowl(int marker) {  
      12.         System.out.println("Bowl(" + marker + ")");  
      13.     }  
      14.   
      15.     void f1(int marker) {  
      16.         System.out.println("f1(" + marker + ")");  
      17.     }  
      18. }  
      19.   
      20. class Table {  
      21.     static Bowl bowl1 = new Bowl(1);  
      22.   
      23.     Table() {  
      24.         System.out.println("Table()");  
      25.         bowl2.f1(1);  
      26.     }  
      27.   
      28.     void f2(int marker) {  
      29.         System.out.println("f2(" + marker + ")");  
      30.     }  
      31.   
      32.     static Bowl bowl2 = new Bowl(2);  
      33. }  
      34.   
      35. class Cupboard {  
      36.     Bowl bowl3 = new Bowl(3);  
      37.     static Bowl bowl4 = new Bowl(4);  
      38.   
      39.     Cupboard() {  
      40.         System.out.println("Cupboard()");  
      41.         bowl4.f1(2);  
      42.     }  
      43.   
      44.     void f3(int marker) {  
      45.         System.out.println("f3(" + marker + ")");  
      46.     }  
      47.   
      48.     static Bowl bowl5 = new Bowl(5);  
      49. }  
      50.   
      51. public class StaticInitialization {  
      52.     public static void main(String[] args) {  
      53.         System.out.println("Creating new Cupboard() in main");  
      54.         new Cupboard();  
      55.         System.out.println("Creating new Cupboard() in main");  
      56.         new Cupboard();  
      57.         table.f2(1);  
      58.         cupboard.f3(1);  
      59.     }  
      60.   
      61.     static Table table = new Table();  
      62.     static Cupboard cupboard = new Cupboard();  
      63. } /* 
      64.  * Output:  
      65.  * Bowl(1) 
      66. Bowl(2) 
      67. Table() 
      68. f1(1) 
      69. Bowl(4) 
      70. Bowl(5) 
      71. Bowl(3) 
      72. Cupboard() 
      73. f1(2) 
      74. Creating new Cupboard() in main 
      75. Bowl(3) 
      76. Cupboard() 
      77. f1(2) 
      78. Creating new Cupboard() in main 
      79. Bowl(3) 
      80. Cupboard() 
      81. f1(2) 
      82. f2(1) 
      83. f3(1) 
      84.  */// :~  

        本站是提供個人知識管理的網(wǎng)絡存儲空間,所有內容均由用戶發(fā)布,不代表本站觀點。請注意甄別內容中的聯(lián)系方式、誘導購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權內容,請點擊一鍵舉報。
        轉藏 分享 獻花(0

        0條評論

        發(fā)表

        請遵守用戶 評論公約

        類似文章 更多