免费视频淫片aa毛片_日韩高清在线亚洲专区vr_日韩大片免费观看视频播放_亚洲欧美国产精品完整版

打開APP
userphoto
未登錄

開通VIP,暢享免費(fèi)電子書等14項(xiàng)超值服

開通VIP
java 初始化總結(jié)

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

一  java初始化基礎(chǔ)知識(shí)
1、 一個(gè)類的所有基本類型數(shù)據(jù)成員都會(huì)保證獲得一個(gè)初始值。 
非基本類型,會(huì)初始化為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. }  


出來(lái)結(jié)果為 

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會(huì)為類的基本類型的變量提供一個(gè)初始值,各類型初始值不同,非基本類型初始為null。注意,這里的變量必須是類變量,注意,只會(huì)為類變量提供初始化,而局部變量不會(huì)。如果局部變量沒有初始化,會(huì)收到一個(gè)出錯(cuò)信息 

 


2、可以通過構(gòu)造方法或其他方法進(jìn)行初始化,但是不會(huì)妨礙java默認(rèn)的初始化

  看下面的例子 

 
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.    


  輸出結(jié)果為 

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


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

 
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.    


  輸出結(jié)果如下 

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


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

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

 
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.    


結(jié)果如下 

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



5、靜態(tài)塊 
  靜態(tài)塊里的變量初始化順序位于普通變量之前,和static變量相比,則是完全由定義的順序來(lái)決定了。另外,靜態(tài)塊里的變量也是只初始化一次,這點(diǎn)和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.    


輸出結(jié)果為 

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


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

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


6、涉及到繼承時(shí) 初始化順序 
  初始化時(shí),如果有static變量或靜態(tài)塊,其初始化順序是位于最前面的,無(wú)論變量位于子類還是父類中,它們二者之間的順序,可參見第5點(diǎn); 
  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.    


輸出結(jié)果如下 

 
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);  

 

}  

 

}  

 

 

 

 

這段程序代碼輸出,實(shí)際運(yùn)行結(jié)果:

 

 

obj.counter1==1

obj.counter2==0

 

 

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

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

 

三  典型初始化例子

 

Java初始話很好的一個(gè)例子, 摘自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. //無(wú)論創(chuàng)建多少對(duì)象, 靜態(tài)數(shù)據(jù)都只占用一份存儲(chǔ)區(qū)域  
  7. //初始化的順序是先靜態(tài)對(duì)象(如果它們尚未因前面的對(duì)象創(chuàng)建過程而被初始化), 而后是"非靜態(tài)"對(duì)象  
  8. //載入.class文件(這將創(chuàng)建Class對(duì)象),有關(guān)靜態(tài)初始化的所有動(dòng)作執(zhí)行.  
  9. //靜態(tài)初始化只在Class對(duì)象首次加載的時(shí)候進(jìn)行一次  
  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.  */// :~  

本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊舉報(bào)。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
Java中static靜態(tài)變量的初始化完全解析
java中普通變量、靜態(tài)變量、靜態(tài)代碼塊初始化的順序辨析
java 難題
【Java】 hashcode()和System.identityHashCode()
xstream實(shí)現(xiàn)JAVA對(duì)象和XML的相互轉(zhuǎn)化
C++中Static作用和使用方法
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長(zhǎng)圖 關(guān)注 下載文章
綁定賬號(hào)成功
后續(xù)可登錄賬號(hào)暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服