Explain the order of execution of the static variable and static block

static {
    name="xiaoming";
}
private static String name=null;

The code above, print out the name is null


private static String name=null;


{static
    name = "xiaoming";
}
The above code, the name is printed xiaoming


static {
    name="xiaoming";
}
private static String name;

The above code, the name is printed xiaoming


Thus, the variable name is first loaded, and the time of the assignment, either directly on the variable assignment or assignment in static code block, are assigned in the order code.

Published 62 original articles · won praise 34 · views 40000 +

Guess you like

Origin blog.csdn.net/qq_42451835/article/details/104246794