Detailed Java static code block, code block non-static, static method, constructor, the code block structure

Java-based students had certainly not open around this small hillside, static code block, non-static block of code, static methods, constructors, construction code blocks, these, which first execution, after the implementation of what, why?

First, start with the next point, then the evidence code, and finally explore the mechanism.

java class load order

1, the virtual machine when it first loads Java classes, have static initialization block static member variables, static methods instance initialization 2 once, and only when you call the new method will create a class 3, class instance creation process: according to his son inherited relationship initialization is first executed initialization block portion of the parent class, then the parent class constructor; and then execute this class inherits initialization block subclass, and finally constructor subclass 4, class instance destroyed when the first destruction sub class section, and then the destruction of part of the parent class

Non-static code block is called each time an object is instantiated. What happens after the new objects: to the object's instance variables (not "constant") allocate memory space, member variables default initialization; Initialization member variable declarations; initialization initialization block (also called a code block or a static configuration code block) ; constructor initialization.

Sample Code

public class Test01 {
    public static void main(String[] args) {
        B b = new B();
    }
}
class B extends A{
    static{
        System.out.println("子类的静态代码块");
    }
    C c = new C();
    {
        System.out.println("子类的构造代码块");
        C.method();
    }
    B(){
        System.out.println("子类的构造方法");
    }
}
class A{
    static{
        System.out.println("父类的静态代码块");
    }
    C c = new C();
    A(){
        System.out.println("父类的构造方法");
    }
}
class C{
    static public  void method(){
        System.out.println("C的静态方法");
    }
    static{
        System.out.println("C类的静态代码块");
    }
    C(){
        System.out.println("C的构造方法");
    }
}

Code running results:

父类的静态代码块
子类的静态代码块
C类的静态代码块
C的构造方法
父类的构造方法
C的构造方法
子类的构造代码块
C的静态方法
子类的构造方法

Interpretation:
The first time you load Java classes, have static initialization block static member variables, static method of time initialization, and B inherits A, first had to perform static code block B, but according to the parent-child relationship is initialized, A added a plug first performs a static block, print "static block of code parent class"; a static block B is also loaded, print "static block of code subclass"; the parent class to class C also load came, static block of code to print "static block of code class C", C new time, configuration method performs printing "configuration method C"; then perform a constructor method a, printing "configuration parent class"; then class B continues downward, a C and a new printing "configuration method C", and then execute the code block non-static downwardly print "configuration code blocks subclass" and "static method C"; the last B constructor for class execution, print "constructor subclass."

mechanism

How to load a specific memory, temporarily not have time to read, TODO, and so see the java class loading mechanism further detailed discussion.

According to the result of decompilation, the entire process is run like this:

public class Test01 {
    public static void main(String[] args) {
        B b = new B();
    }
}
class B extends A{
    static{
        System.out.println("子类的静态");
    }
    C c ;

    B(){
        super();
        c = new C();
        System.out.println("子类的构造代码块");
        C.method();
        System.out.println("子类的构造方法");
    }
    public void method(){
        System.out.println("子类的成员方法");
    }
}
class C{
    static public  void method(){
        System.out.println("C的静态方法");
    }
    static{
        System.out.println("C类的静态");
    }
    C(){
        super();

        System.out.println("C的构造方法");
    }
}
class A{
    static{
        System.out.println("父类的静态");
    }
    C c ;
    A(){
        super();
        c = new C();
        System.out.println("父类的构造方法");
    }
}

Construction method which, prior Super (), then member variables (c = new C ()), then other code, the operation is completed, sequentially read. This is the real memory operating rules.

Spread

Look at the following questions, may I ask what is the output

public class Test03 {
    public static void main(String[] args) {
        Zi zi = new Zi();
    }
}
class Zi extends Fu{
    public int count=3;
    public Zi(){
    }
    public void print(){
        System.out.println("Zi..."+count);
    }
}
class Fu{
    public int count=10;
    public Fu(){
        print();
    }
    public void print(){
        System.out.println("Fu..."+count);
    }
}

As before logic, after the transformation code is as follows:

public class Test03 {
    public static void main(String[] args) {
        Zi zi = new Zi();
    }
}
class Zi extends Fu{
    public int count=0;//默认初始值为0
    public Zi(){
        super();
        count=3;
    }
    public void print(){
        System.out.println("Zi..."+count);
    }
}
class Fu{
    public int count=0;
    public Fu(){
        super();
        count=10;
        print();
    }
    public void print(){
        System.out.println("Fu..."+count);
    }
}

Zi zi = new Zi (), find the last constructor subclass, Super (), this time count or subclass 0, find the parent class constructor of the parent class, the count to 10, print (), sub rewrite the class they have to find their own subclass, count also find their own subclass, or 0 at this time, resultZi...0

Guess you like

Origin blog.csdn.net/wjl31802/article/details/90199020