Static block initialization

前端 未结 3 1545
慢半拍i
慢半拍i 2021-01-19 05:19

This is a snippet of Java code:

static {        
    ture = 9;       
}
static int ture;
{ // instance block 
    System.out.println(\":\"+ture+\":\");              


        
3条回答
  •  春和景丽
    2021-01-19 05:43

    The static initializers are executed in the order in which they appear and the declarations aren't executed at all, that's how they got their name. This is why your code compiles without problems: the class structure is assembled at compile time from the declarations, and the static blocks are executed at runtime, long after all the declarations have been processed.

提交回复
热议问题