The execution order of free blocks in Java _java

Source: Internet
Author: User
    • 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-----------

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.