静态变量的编译过程

  刚刚在看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

阅读更多
文章标签: class string
个人分类: JAVA
上一篇郁闷的访问mysql数据库
下一篇String和StringBuffer
想对作者说点什么? 我来说一句

了解wince编译过程

2009年07月21日 29KB 下载

mtk编译过程.docmtk编译过程.doc

2011年05月18日 49KB 下载

MTK 编译过程

2009年12月18日 119KB 下载

没有更多推荐了,返回首页

关闭
关闭