1. First list the code
Base.java
public class base{ Base () { preprocess (); } void Preprocess () {}}
Derived.java
public class Derived extends base{public String whenamiset = "set when declared"; @Override void Preprocess () { Whenamiset = "set in Preprocess ()";} }
Main.java
public class main{public static void Main (string[] args) { Derived d = new Derived (); System.out.println (D.whenamiset); }}
2. Implementation results
3. Reason
The http://blog.csdn.net/jltxgcy/article/details/17398751 can be tested.
1, call the parent class global variable initialization, there are no global variables and do not have to initialize.
2, call the parent class constructor, inside called the subclass member function PREPROCESS,WHEnamiset is set to "set in Preprocess".
3, call the subclass global variable initialization, Whenamiset is set to "set when declared".
4. Call the constructor of the child class.
4, Pan outside
Assuming that the global variable is whenamiset, no initialization value is set. Then it is set to the default value.
The object is null,int to 0.
Local variables. The initialization value is not set. The settings need to be displayed and cannot be used directly.
The C + + constructor initialization list must display an initialization parameter.
is different from Java.
A trap for member initialization when Java constructs