- The free blocks in Java are divided into static free blocks and non-static free blocks.
- The execution time of a non-static free block is: Before the constructor is executed.
- The execution time of the static free block is: The class file executes when it is loaded.
- A non-static free block can be executed multiple times, as long as an object is initialized, but the static free block is executed once at the time the class is loaded, and is typically used to initialize the value of the static variable of the class.
- Each initialization of an object results in the execution of a non-static block.
- If inheritance is involved, then the non-static block of the parent class is executed first, then the constructor of the parent class, then its own free block, and finally its own constructor.
- The execution time of a static block is when the class file is loaded, the class file is loaded only once, so the static block is executed once, and then the static block is not executed when the class is used later.
- The execution time of a static block is the initialization phase after class loading. The execution of a static block is not triggered if the ClassLoader loadclass is used to simply load the class without initializing it. Forname (String) with class takes the default initialize to True, that is, initialization. If you set loader to False by using forname (String Name,boolean Initialize, ClassLoader initialize), the static block is not executed.
- The initialization phase after performing class loading includes: Running <clinit> methods, which are initialization statements and static free block statements for class variables. This method is generated by the Java compiler gathering information and cannot be displayed.
Here are examples to illustrate:
Parent class
Copy Code code as follows:
Father.java
public class Father {
static{//static block
System.out.println ("Father ' sstatic free block Running");
}
{//non-static block
System.out.println ("Father ' Sfree block running");
}
Public father () {
System.out.println ("Father ' sconstructor running");
}
}
Sub Class
Copy Code code as follows:
Son.java
public class son extends father{
static{//static block
System.out.println ("Son ' sstatic free block Running");
}
{//non-static block
System.out.println ("Son ' s freeblock running");
}
Public Son () {
TODO auto-generated Constructor stub
System.out.println ("Son ' Sconstructor running");
}
}
class where the main function resides
Copy Code code as follows:
Test.java
public class test{
public static void Main (string[] args) {
Class F;
try {
System.out.println ("--------beforeload Father--------");
F=class.forname ("Freeblock.father");
System.out.println ("--------afterload father---------");
System.out.println ("--------beforeinitial Father Object--------");
F.newinstance ();
System.out.println ("--------afterinitial Father Object--------");
catch (ClassNotFoundException e) {
E.printstacktrace ();
catch (Instantiationexception e) {
E.printstacktrace ();
catch (Illegalaccessexception e) {
E.printstacktrace ();
}
Class s;
try {
System.out.println ("-------beforeload son--------");
S=class.forname ("Freeblock.son");
System.out.println ("--------afterload son-------");
System.out.println ("--------beforeinitial son object----------");
S.newinstance ();
System.out.println ("--------afterinitial son object-----------");
catch (ClassNotFoundException e) {
E.printstacktrace ();
catch (Instantiationexception e) {
E.printstacktrace ();
catch (Illegalaccessexception e) {
E.printstacktrace ();
}
}
}
Execution results:
--------before Loadfather--------
Father ' s STATIC free blockrunning
--------after Loadfather---------
--------before initial fatherobject--------
Father ' s free block running
Father ' s constructor running
--------after initial fatherobject--------
-------before load son--------
Son ' s STATIC free block running
--------after load son-------
--------before initial sonobject----------
Father ' s free block running
Father ' s constructor running
Son ' s free block running
Son ' s constructor running
--------after initial son object-----------