java 静态块和构造块等执行顺序分析学习

构造块:在类成员变量区域用大括号括起来的代码,和方法相比没有修饰,没有返回,没有参数;
静态块:在构造块前加 static修饰
静态代码块:静态块+静态变量
非静态代码块:普通类成员变量+构造块


程序执行顺序:静态代码块->非静态代码块->类构造方法

public class Test {
  
      static {                
          System.out.println("静态块");
      } 
      
      {                 
          System.out.println("构造块");
      }
    
     public Test(String str) {           
         System.out.println("构造函数"+str);       
     }  
    
     public static void main(String[] strings) {           
         Test t1 = new Test("t1"); 
         Test t2 = new Test("t2");         
     }   
 }

执行结果为:

静态块
构造块
构造函数t1
构造块
构造函数t2

也就是说静态块会先于main函数执行;(没有main函数,程序还能执行吗?)
然后是main里面实例化对象,这时程序会跳到构造函数那一行,但不会直接执行构造函数中的内容,而是开始执行非静态代码块,等执行完这一部分之后再执行构造函数;
还有一点,从结果可以看出静态代码块只执行了一次。

有一种特殊情况,静态对象
在静态变量中new一个自身的对象

public class Test {

    public static int k = 0;     
    
    public static Test t1 = new Test("t1");       
    
    public static int i = print("i"); 
    public static int n = 99;
    
    public int y=0;
    static {     
        System.out.println("静态块");
        } 
    
    public int y1=0;
    public int y2=0;
    public int y3=0;
    public int y4=0;
    public int j1 = print("j1"); 
    {          
        System.out.println("构造块");
    }
    

    public Test(String str) {           
        System.out.println((++k) + ":" + str + " i=" + i + " n=" + n);          
        ++i;           
        ++n;       
    }    
    
    public static int print(String str) {           
        System.out.println((++k) + ":" + str + " i=" + i +" n=" + n);          
        ++n;           
        return ++i;           
    }      
    
    public static void main(String[] strings) {           
        Test t = new Test("init");       
    }   

}

执行结果:

1:j1 i=0 n=0
构造块
2:t1 i=1 n=1
3:i i=2 n=2
静态块
4:j1 i=3 n=99
构造块
5:init i=4 n=100

从结果中我们发现,”静态块“居然不是第一个输出,debug调试后发现执行顺序是
1.public static int k = 0;
2.public static Test t1 = new Test("t1");
3.public Test(String str) //构造函数,但不执行里面的内容
4.public int y=0;
5.public int y1=0;
...
6.public int j1 = print("j1");
7.System.out.println("构造块");
8.public Test(String str) { ...}//构造函数里面的内容

确实是先执行了静态块,不过遇到静态变量时
程序会先跳到构造函数,但不执行里面的内容,然后开始执行非静态代码块(注意 public int y=0; 也是非静态代码块中的一份子)
然后执行构造函数
所以才会出现了这样的结果

以下是执行顺序,抽之间重新画一下

 



以下是阿里的一到面试题
比上面的多一个地方
静态块和构造块中内容都是输出,在构造块之后又new了一个t2对象
不过没有关系,用上面的分析依然可以得到结果

public class Test {

  public static int k = 0; 

  public static Test t1 = new Test("t1"); 
  
  public static int i = print("i"); 
  public static int n = 99;

  static { 
    print("静态块"); 
  } 

  public int j1 = print("j1"); 
  { 
    print("构造块"); 
  }

  public int j = print("j"); 

  public static Test t2 = new Test("t2");

  public Test(String str) { 
    System.out.println((++k) + ":" + str + " i=" + i + " n=" + n); 
    ++i; 
    ++n; 
  } 

  public static int print(String str) { 
    System.out.println((++k) + ":" + str + " i=" + i +" n=" + n); 
    ++n; 
    return ++i; 
  } 

  public static void main(String[] strings) { 
  Test t = new Test("init"); 
  } 
}

结果为:

1:j1 i=0 n=0
2:构造块 i=1 n=1
3:j i=2 n=2
4:t1 i=3 n=3
5:i i=4 n=4
6:静态块 i=5 n=99
7:j1 i=6 n=100
8:构造块 i=7 n=101
9:j i=8 n=102
10:t2 i=9 n=103
11:j1 i=10 n=104
12:构造块 i=11 n=105
13:j i=12 n=106
14:init i=13 n=107

  



第一行输入代码为
public int j = print("j");
-->System.out.println((++k) + ":" + str + " i=" + i +" n=" + n);

这里输出了i的值为0,根据上面的分析,变量i的初始化即
public static int i = print("i");
这一行还没有执行到,那为什么不报错呢?

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值