Static blocks are constructors within a class, and objects have constructors, so classes have constructors, and constructors inside classes are called initialization methods. That is, the new object. He passes through a constructor. Load a class, and there is a piece of code that is initialized, which is called a static block. A class can have a lot of static blocks, the static block of the running process is, if a class has more than one static block, then he is running from top to bottom. 7.5 is shown below:
650) this.width=650; "Src=" http://dl2.iteye.com/upload/attachment/0121/0348/ 33554224-6f69-37f1-8843-72b6286c2076.png "style=" border:0px; "/>
Figure 7.5
He runs from top to bottom, but when he needs to be loaded, he is instantiated, but static blocks are executed from top to bottom. It can contain many code downloads, but static blocks cannot be accessed by members of the instance. 7.51 is shown below:
650) this.width=650; "Src=" http://dl2.iteye.com/upload/attachment/0121/0350/ 9ace1efe-7fe2-34cf-adc5-0fc7b35c1287.png "style=" border:0px; "/>
Figure 7.51
Static blocks are created when a class is loaded, and static blocks are loaded in 4 ways. The first static block of the new object initializes the firing class to be loaded. 7.52 is shown below:
650) this.width=650; "Src=" http://dl2.iteye.com/upload/attachment/0121/0352/ F0a8606e-c8a3-31fa-9e47-12892af08909.png "style=" border:0px; "/>
Figure 7.52
The second access to the static property also fires the class to be loaded. 7.53 is shown below:
650) this.width=650; "Src=" http://dl2.iteye.com/upload/attachment/0121/0354/ A3f2cb89-2ef2-3e43-af07-5dcefbb3b2fd.png "style=" border:0px; "/>
Figure 7.53
The third invocation of the static method inside will also touch the class to load it up. 7.54 is shown below:
650) this.width=650; "src=" Http://img.mp.itc.cn/upload/20160901/01a7273fe23e49d4b7e483c7b52f3ec3.jpg "style=" border:0px;font-size:0px;margin:0px;padding:0px; "/>
Figure 7.54
The fourth type uses Class.forName ("Test"), which can be loaded by string, which is called dynamic loading, or a class can be loaded by a touch. 7.55 shows: Download
Figure 7.55
A constant member cannot be initialized with load 7.56 as shown:
Figure 7.56
The print is 10, but the static block of something is not printed out, it means that the class is not loaded, because he is a static constant, static constants do not belong to the memory of the class, he is not allocated in this class memory, he does not need to initialize this class. Static properties can be, because the static property memory must be allocated in this class, so he will be initialized to load, so the static block in the diagram will not be loaded.
Java Static block Learning