CSDN博客

img yoti

静态变量的编译过程

发表于2004/10/1 21:30:00  675人阅读

分类: JAVA

  刚刚在看http://community.csdn.net/Expert/topic/2872/2872127.xml这个非常长但非常有意义的贴子,看到里面的一段关于讨论下面这段代码内容:
class Singleton {                                                        
                                               
  private static Singleton obj = new Singleton();                        
                                               
  public static int counter1;                                            
                                               
  public static int counter2 = 0;                                        
                                               
  private Singleton() {           
    //System.out.println("counter1:" + counter1 + "/n" + "counter2:" + counter2);                                       
                                               
    counter1++;                                                          
                                               
    counter2++;    
    //System.out.println("counter1:" + counter1 + "/n" + "counter2:" + 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
去掉代码里面两条调试语句前的注释后结果为:
counter1:0
counter2:0
counter1:1
counter2:1
obj.counter1==1
obj.counter2==0
将程序改动一下,马上就会明白编译的顺序了:

class Singleton {                                                        
                                               
  private static Singleton obj = new Singleton();                        
                                               
  public static int counter1;                                            
                                               
  public static int counter2;//开始改动

  static {
    counter2 = 1;
  }                                  //改动结束
                                                
  private Singleton() {          
    System.out.println("counter1:" + counter1 + "/n" + "counter2:" + counter2);                                       
                                               
    counter1++;                                                          
                                               
    counter2++;    
    System.out.println("counter1:" + counter1 + "/n" + "counter2:" + 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);                   
                                               
  }                                                                      
                                               
}
改动后结果为:
counter1:0
counter2:0
counter1:1
counter2:1
obj.counter1==1
obj.counter2==0

阅读全文
0 0

相关文章推荐

img
取 消
img