Java Interview Literacy: Object initialization order (really not necessarily correct)

Source: Internet
Author: User

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?
    1. Put forward conjecture: Parent class static Property = Parent Property = = Parent class constructor method = > Subclass Static property = Subclass Attribute = = Subclass Constructor method?
    2. 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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.