Class initialization problem

public class Main extends A{
    
    private String s = "hello";
    public Main(){
        hello();
    }
    public void hello(){
        System.out.println("s "+s);
    }
    public static void main(String[] args) {
        new Main();
    
    }
}

class A{
    private String s2 = "world";
    public A(){
        hello();
        System.out.println(this);
    }
    public void hello(){
        System.out.println("s2 "+s2);
    }
}
Results 
S null 
com.briup.Main@15db9742 
S Hello

 

Resolution:

Initialization process is as follows: 
1. First, a static member variable is initialized in the parent class and a static code block, in order of appearance in the program initialization; 
2. Then, the static member variable initialization subclasses and static code block, in order of appearance in the program initialization; 
3. Next, the block and member variables common parent class initialization, the execution of the parent class constructor;
4. Finally, common member variables and initializes subclass code blocks constructed in subclass method performed;

Guess you like

Origin www.cnblogs.com/wskb/p/10958923.html