/
What is the execution result of the current method? Please outline the principle
/
Class Demo1 {
static {
Demo2 d = new Demo2 ();
}
{
System.out.println ("I am the DEMO1 code block");
}
Public Demo1 () {
System.out.println ("I am the Demo1 method of construction");
}
}
Class Demo2 {
static {
Demo1 d = new Demo1 ();
}
{
System.out.println ("I am the DEMO2 code block");
}
Public Demo2 () {
System.out.println ("I am the Demo2 method of construction");
}
}
Class Test {
public static void Main (string[] args) {
Demo2 D2 = new Demo2 ();
Demo1 D1 = new Demo1 ();
}
}
The execution results are:
I'm Demo2 's construction code block.
I'm the Demo2 method.
I'm Demo1 's construction code block.
I'm the Demo1 method.
I'm Demo2 's construction code block.
I'm the Demo2 method.
I'm Demo1 's construction code block.
I'm the Demo1 method.
Principle of execution:
1. The program enters the main method, and the program jumps to Demo2 d2 = new Demo2 ();
Here to create the Demo2 class object, you need to go into class Demo2 inside the
2. The program enters Class Demo2, where the static statically constructed code block
program executes the statement inside static, Demo1 d = new Demo1 (); The program jumps to the class Demo1
(the code block in Demo2 and the construction method are temporarily shelved after static execution)
3. The current program has entered the class Demo1, where static statically constructed code blocks exist
Executes the statement inside the static, Demo2 d = new Demo2 (); The program jumps to class
(the code block and construction method in Demo1 are temporarily shelved and executed again after static)
- The current program enters the class Demo2, the static code block executes, no longer executes, the construction code block does not execute independently
so the program jumps to public Demo2 (), does not enter the inside of the method, but calls the construction code block
This executes the block of construction code in Demo2, and the program outputs the first line of text:
------------------------------------------"I am Demo2 's construction code block"
- The current program starts executing the statement in the constructor after executing the code block in the Demo2, outputting the second line of text:
------------------------------------------"I am Demo2 's constructor"
- The point is, the program from here after the execution of the jump again, jump to the second place to shelve the code, that is,
Jump back to class Demo1 inside, execute the statement just put on hold, output constructs the code block construction method statement:
------------------- ------------------------"I am Demo1 's construction code block"
-------------------------------------------"I am the Demo1 method"
- After the execution of the shelved code in class Demo1, the program moves to the place where the code was first shelved
that is, the first time you enter Class Demo2 execute a static block of code, execute the shelved statement:
block of code and construct method
output two lines of statement:
- ------------------------------------------"I am Demo2 's construction code block"
-------------------------------------------" I'm Demo2 's constructor. "
here the first line of the main method is completely executed
7. Next the program jumps to the second line of the statement Demo1 D1 = new Demo1 ();
Go inside the class Demo1 and jump into the static code block and find that the static code block has been executed and no longer executes
The program jumps to the construction method, executes the construction code block, and finally executes the construction method
The program outputs statements separately
-------------------------------------------"I am a DEMO1 code block"
-------------------------------------------"I'm the Demo1 of the building."
8. Program execution Complete
---------------------
Szw ' S
Source: CSDN
Original: 83243832
Copyright NOTICE: This article is for bloggers original article, reprint please attach blog link!
Ps:szw ' s Just me ~
Statically constructing blocks of code, constructing blocks of code, ordering problems in the execution of construction methods