The Java object Construction process

Source: Internet
Author: User

to run a piece of code first
Class A {public A () {init ();} public void init () {}public static void main (string[] args) {b b = new B (); System.out.println ("Finally the value of I is:" + B.I + ", the value of J is:" + B.J);}} Class B extends A {int I;int j = 999;public void init () {System.out.println ("a constructor is calling this method at this time: the value of I is:" + i + ", the value of J is:" + j); = 888;j = 111;}}
See what you print .
At this point the constructor of a is calling this method: The value of I is: 0,j is: 0 The value of finally I is: 888,j value is: 999
If you are surprised, you are not familiar with the Java object construction process. Reading this article so seriously will help you a lot.

Write a case code first
public class Super {    static long time = 10;    static Object obj = new Object ();  &nb Sp  int width = 100;    static {        time = 11;    }& nbsp;   {        width = 110;    }    public Super () {        width = 120;    }    public static void Main (string[] args) {        child child = new Child ();       & nbsp System.out.println ("Super.time:" +super.time);        system.out.println ("Super.obj : "+super.obj);        system.out.println (" Child.width: "+child.width);        system.out.println ("Child.age:" +child.age);         System.out.println ("Child.str:" +child.str);     &NBSp  system.out.println ("Child.height:" +child.height);    }}class Child extends Super {     static int age = 20;    static String str = "str";    double height = 200;    static {        age = 22;    }    {        height = 210;    }    public child () {         height = 220;    }}

Print

Super.time:11super.obj:[email protected]child.width:120child.age:22child.str:strchild.height:220.0
the construction process of an object in Java
1. Load the parent class with the class loader. Allocates space for all static variables of the parent class in the order defined by the parent class static variable and assigns the parent class static variable default value
public class Super {static Long time=10;//at this time Time=0static object Obj=new object ();//At this time Obj=null


2. Load yourself with a class loader, allocate space for all of your static variables in the order defined by your own static variables, and give yourself static variable default values
Class Child extends Super{static int age=20;//at this time age=0static String str= "str";//At this time Str=null


3. Assign defined values to all static variables of the parent class in the order defined by the parent class static variables
public class Super {    static long time=10;//at this time time=10    static Object Obj=new object ();//At this time Obj=new object ()


4. Run the parent class static code block
public class Super {static Long Time=10;static object Obj=new object (); int width=100;static{time=11;//static code block is running. This time time=11}


5. Assign a defined value to all of your static variables in the order defined by your own static variables
Class Child extends Super{static int age=20;//at this time age=20static String str= "str";//At this time str= "str"


6. Run your own static block of code
Class Child extends Super{static int age=20;static String str= "str";d ouble height=200;static{age=22;//at this time age=22}

7. Allocate space for the parent class instance variable. and give the default value
public class Super {static Long Time=10;static object Obj=new object (), int width=100;//at this time width=0

8. Allocate space for your instance variables. and give the default value
Class Child extends Super{static int age=20;static String str= "str";d ouble height=200;//at this time height=0.0


9. Assign defined values to all instance variables of the parent class in the order defined by the parent class instance variable
public class Super {static Long Time=10;static object Obj=new object (), int width=100;//at this time width=100


10. Run the construction code block of the parent class
public class Super {static Long Time=10;static object Obj=new object (); int width=100;static{time=11;} {width=110;//at this time width=110}


11. To run the parent class's construction method
public class Super {static Long Time=10;static object Obj=new object (); int width=100;static{time=11;} {width=110;} Public Super () {width=120;//at this time width=120}


12. Assign a defined value to all of your instance variables in the order defined by their instance variables
Class Child extends Super{static int age=20;static String str= "str";d ouble height=200;//at this time height=200.0


13. Run your own block of construction code
Class Child extends Super{static int age=20;static String str= "str";d ouble height=200;static{age=22;} {height=210;//at this time height=210.0}


14. Run your own construction method
Class Child extends Super{static int age=20;static String str= "str";d ouble height=200;static{age=22;} {height=210;} Public Child () {height=220;//at this time height=220.0}


Object Construction finished!


Note1-6 belongs to the initialization static part, 7-14 belongs to the initialization instance part


Suppose that the static part of a class has already been initialized (loaded by the class loader). The static part is not repeated again, and the initialization of the static part is initialized only once when the class loader loads a class.
The parent class assumes that there is also a parent class that initializes the parent class of the parent class in this order until object


Assuming that the class of the assigned value is not initialized when the 3,5,9,12 assignment operation is run, the referenced class is initialized first, assuming that the referenced class and the referenced class also initialize the reference class of the reference class in this order. Until all referenced classes have been initialized

Like what:

We define a reference to Class B in Class A.


public class Super {public static void main (string[] args) {new A (); new B ();}} Class A{static B b=new b ();//This Code causes Class B to be initialized before class A, that is, the static property of B is assigned first, and the static code block runs first. static {System.out.println ("AA");}} Class B{static {System.out.println ("BB");}}

Print:

Bbaa


simply defining a reference to a class without assigning a value will not trigger a class initialization

public class Super {public static void main (string[] args) {new A ();}} Class A{static B b;static {System.out.println ("AA");}} Class B{static {System.out.println ("BB");}}

Print:

Aa

Only active use is triggered, which causes the referenced class to be initialized.

About a person in what situation is active use please check out one of my articles:

http://blog.csdn.net/u012643122/article/details/46522345


If a class A refers to Class A, which refers to the case of a recursive reference, then the Java elimination recursion mechanism is implemented .

Such as

public class Super {public static void main (string[] args) {new A ();}} Class A{static B b=new b (); static {System.out.println ("AA");}} Class b{static a a=new a (); static {System.out.println ("BB");}}

Print

Bbaa

initializing a referenced class is like walking a path, and Java avoids recursion by the way it has gone before.


assume that you are running 3, 5, 9, 12 o'clock. The variable will remain the default value if the discovery variable simply defines a reference and does not have an assignment operation .

Such as:

The static long time;//retains the previously assigned default value of 0Child child;//before preserving the default value assigned to NULL


steps to omit for special cases

Assuming a class has no parent class (such as the object class), its initialization order can be simplified to 2, 5, 6, 8, 12, 13, 14.

Assuming that the class has been loaded by the class loader, that is, the static part of the class has already been initialized, then 1, 2, 3, 4, 5, 6 will not run, the total order can be reduced to 7, 8, 9, 10, 11, 12, 13, 14.

Suppose this class is not loaded by the class loader, but its parent class has already been loaded by the class loader. Then the overall order can be simplified to 2, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14.




Reprint please indicate the original address. Please respect the original, thank you!



The Java object Construction process

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.