Discussion on the call sequence of static variables, member variables, and parent-child constructor methods of Java inherited classes

Source: Internet
Author: User

Based on relevant posts on the Internet and my own debugging, I have studied static variables, member variables, and the sequence of calling methods for the parent and child classes of the Java inheritance class. First, let's look at a program:

 

Class X {
Y B = New Y (); // 7. initialize the parent class member variable.
Static y Sb = New Y (); // 1. Static variables of the parent class, static y output (static code block initialized first), 2. Y

Static {
System. Out. println ("static x parent class static code block"); // 3. Execute static code block
New Y (); // 4. Here, only y is output and static y is generated (the static code block is executed only once)
}
X (){

System. Out. println ("X"); // 8. After the parent class member variable is initialized, execute the parent class constructor to output x
}
}

Class y {
Static {
System. Out. println ("static y ");
}
Y () {// execute the constructor
// Here there is a super () = Object ()
System. Out. println ("Y ");
}
}

Public Class Z extends X {
Final Static int Mead = 45;
Final byte B = 16;
Static y Sb = New Y (); // 5. The static variable of the subclass.
Static {
System. Out. println ("static Z"); // 6. Static code block of the subclass
}
Y = New Y (); // 9. initialize the member variable of the subclass.

Z (){
// Here there is super () = new x ()
This. Y = NULL;
System. Out. println ("Z"); // 10. After the subclass member variable is initialized, execute the subclass constructor to output Z
}

Public static void main (string [] ARGs ){
New Z ();

}
}

 

Execution result:

Static y
Y
Static x parent static code block
Y
Y
Static Z
Y
X
Y
Z

 

Explanation:
Static objects need to access the memory during compilation. Their Initialization is earlier than non-static objects, and the sequence is parent class and Child class.
Initialize the class. First, execute the constructor of the Super () parent class (after final and static). Then, the constructor of the parent class executes super () until the object super (), initialize General member variables

Generally, the initialization of member variables is complete and the code in the constructor (code after super () is executed ).
After the initialization of the parent class is complete (Super execution in the subclass constructor is complete), it is the turn of the member variable initialization of the subclass.
After the subclass member variable Initialization is complete, run the code in the subclass Constructor (code after super ).

 

Note:

The Calling sequence of static blocks and static variables is executed in the writing order. For example, the writing sequence of static blocks and static variables in the preceding class X is reversed as follows:

Class X {
Y B = New Y ();
Static {
System. Out. println ("static x parent static code block ");
New Y ();
}
Static y Sb = New Y ();
X (){
System. Out. println ("X ");
}

}

The execution result is:

Static x parent static code block
Static y
Y
Y
Y
Static Z
Y
X
Y
Z

 

Finally:

The variable space and Initial Value assignment are separated. First, the static member variable space is determined at a time and assigned to binary 0. Then, values are assigned one by one in the writing order.

The following code outputs 0.0

Public class test {
Static int I = f ();
Static double D = 0.1234;
Public static void main (string [] ARGs ){
New Test ();
}
Static int F (){
System. Out. println (d );
Return 3;
}
}

 

 

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.