There are three concepts that need to be understood first:
A Static initialization: Refers to the execution of content inside a static initialization block.
Two Instance initialization: Refers to the execution of the contents of an instance initialization block.
Three Constructor: A method with the same name as the name of the class, in particular, with no return value.
Let's take a look at the program results:
Packagecom; classbook{ Public Static intbooksum=0;//Static Variables Static{//This is a static initialization blockprint (); System.out.println ("This is static block"); } {//Instance initialization blockSystem.out.println ("Initialization block:" +booksum); } PublicBook () {//Construction MethodSystem.out.println ("This was book ' s constructor~"); Booksum+=1; } Public Static voidPrint () {//Static MethodsSystem.out.println ("This is a static method~"); } Public Static voidMain (String []args) {//Book book=new Book (); } }
Execution Result:
This is static method~
This is static block
If you put
Public Static void Main (String []args) { // }
Note, the result of the operation is:
This is static method~
This is static block
Initialization block: 0
This was book ' s constructor~
Summarize:
The Java load class, the process of creating objects is explored only from the point of view of code execution, and does not go deep into the JVM mechanism.
1. The first time an object is created, the class to which the object belongs is loaded, that is, the corresponding. class file, and of course, if the class is already loaded and the object of the class is created again, the class is no longer needed. When a class loads, there are three parts that need to be loaded, a static variable, then a static method, and then a static initialization block. (See the first execution results, because there is no instance created so the initialization block does not execute)
2. It then starts to create an instance of the class, of course, if the static method is created with the object in the static initialization object, the class of the object continues to load, and the class of the object is loaded, of course, without having to load it again. So what is the process of creating an object instance? The first is the introduction of member variables, and then the instance initialization block, then the construction method, the construction method after the completion of the object to be created.
In this process, there are three, static initialization, instance initialization, and construction methods where you can actually write code execution. From the above analysis, we can see that the execution order of the three code blocks is the static initialization of the first class, then the initialization of the instance, and finally the execution of the construction method. That is, static initialization is a process of class loading, so it executes only once, and instance initialization is performed once per object creation, and the constructor is actually the same as the instance initialization, but it executes after the instance is initialized, and the construction method can overload multiple Which construction method to execute is based on your choice.
You can add a statement to the main () function to see:
Public Static void Main (String []args) { book book =New book (); Book Book1=new book (); }
At this point, the result is:
This is static method~
This is static block
Initialization block: 0
This was book ' s constructor~
Initialization block: 1
This was book ' s constructor~
This means that instance initialization is performed every time the object is created.
Java static initialization, instance initialization, and method of construction