Execution order of the static code block, code block non-static, constructors

父类:FatherStaticDemo


FatherStaticDemo class {public
    {
        System.out.println ( "Father normal code block");
    }
 
    static {
        System.out.println ( "Father static code block");
    }
 
    public FatherStaticDemo () {
        System.out.println ( "Parent class constructor ");
    }
}


子 类: SonStaticDemo


SonStaticDemo the extends FatherStaticDemo {class public
    {
        System.out.println ( "Son normal block");
    }
    
    static {
        System.out.println ( "Son static code block");
    }
    
    public SonStaticDemo () {
        System.out.println ( "subclass constructor");
    }
}


Test categories: Test

the Test class {public
    {
        System.out.println ( "Common test class code block");
    }
    
    static {
        System.out.println ( "static code block test class");
    }
    
    public static void main (String [] args ) {
        SonStaticDemo new new SonStaticDemo S = ();
    }
}

Results of the:

 


to sum up:

Static code block is executed when the class is loaded and executed before block non-static

Non-static block, when executed in creating the class object, create an object is not performed is not (such as: non-static test class code block is not performed)

Constructors last execution
 

Parent class static block of code -> subclass static code block

-> parent class non-static block -> parent class constructor

-> non-static block subclass -> subclass constructor

 

 

 

java, when creating an object instance of a class using the new operator, the space and begin dispensing member variable is initialized to a default value refers to note here is not variable is initialized to an initial value at a variable definition, but to shaping assigned 0, a null string assigned to this different C ++, (student.name = null, student.age = 0)

 

Then enter the class constructor.

In the constructor function, we must first check whether this or super call, this call is completed calls between the present class constructor itself, super call is called through the parent class. Both can appear only one, and only as an occurrence of the constructor. Jump to realize the program when calling this and super and instead perform this constructor is called or the super constructor.

In this and finished super, variable initialization program executed instead work undertaken in the class definition.

This is finished, the constructor is executed in the rest of the code.

 

Summary: static code block is always executed first.

          Non-static block of code, like non-static method, with the object concerned. Only non-static block of code before the constructor.

          Parent non-static block, the constructor is finished (corresponding parent class object initialization is completed), it starts executing code blocks and non-static constructors subclasses.

 

================================================================================

The same point: all constructors and before execution in the JVM loads class can be defined in a plurality of classes,

Usually some static variables in the code block assignment.

Different points: a static block of code before the code block non-static

(Static block of code -> non-static block -> Constructor).

Static block of code is executed only when the first class loader is loaded once and then not performed, rather than static code block is executed once every time new. Non-static block may be defined (but little effect) in the conventional method; and not a static block.

 

JVM executes the static loading class code block, if a plurality of static block, the JVM sequentially executes them in the order they appear in the class, each code block is executed only once.

Refer to the original: https: //blog.csdn.net/dhj1235/article/details/81561021
 

Guess you like

Origin blog.csdn.net/weixin_44018338/article/details/91593263