public class TestCodeSeg{static{System.out.println("1");}{System.out.println("2");}public TestCodeSeg(){System.err.println("3");}public static void main(String[] args){new TestCodeSeg();}}
The sequence 1 and 2 are unchanged because they are static (executed during class loading) and 3 is in the constructor. Therefore, the execution sequence of the program is the static part and then the constructor. However, the output results will also be affected by system. Out and system. Err. System. Out is a row buffer while system. Err is not a buffer, so system. Err. println ("3") is an immediate output. However, system. Out. println ("1") and system. Out. println ("2") may be later due to buffering (but the order of 1 and 2 remains unchanged ). The specific output result 1 is absolute after 2, but where 3 appears is related to the execution of the program. If system. Err. println ("3") is executed, 1 and 2 are not output yet, then 3 first:
3
2
1
If 1 has been output but the line break has not been output, it is:
13
2
And so on:
1
3
2
And so on
Reprinted: http://bbs.csdn.net/topics/390420249