Record the interview encountered in some of the Java blind area, on the one hand scan their knowledge blind area, on the one hand can also give the back of the interview friends some warning, so as to avoid the interview when there is no known embarrassing situation.
Ask the question: Parent class static property, Parent class property, parent class constructor method, subclass static property, subclass attribute, subclass constructor method initialization order?
- Put forward conjecture: Parent class static Property = Parent Property = = Parent class constructor method = > Subclass Static property = Subclass Attribute = = Subclass Constructor method?
- Methodology: Practice is the only criterion for testing truth.
The structure of the preliminary design class UML.
Directly on the source code
/** * 轮子类 */public class Wheel { public Wheel(String name){ System.out.println(name); }}
//汽车public class Car { static Wheel backWheel = new Wheel("static: Car => backWheel"); Wheel forwordWheel = new Wheel("Car => forwordWheel"); Car(){ System.out.println("Car"); }}
//奔驰汽车public class BenzCar extends Car{ static Wheel backWheel = new Wheel("static: BenzCar = > backWheel"); Wheel forwordWheel = new Wheel("forwordWheel = >"); BenzCar(){ System.out.println("BenzCar"); }}
//测试类public class Main { public static void main(String[] args) { System.out.println("====================>"); BenzCar benzCar = new BenzCar(); System.out.println("====================>"); }}
Output: a bit unexpected.
====================>static: Car => backWheelstatic: BenzCar = > backWheelCar => forwordWheelCarforwordWheel = >BenzCar====================>
java学习群669823128
Conclusion: conjecture error, parent static property = = Subclass static Property = = Parent Property = = Parent class constructor Method = Subclass Property = = Subclass Constructor method. And there is a detail that static variable initialization is not initialized at first. It's time to instantiate. The static property only starts initializing.
Java Interview Literacy: Object initialization order (really not necessarily correct)