The various member initialization order of Java class, such as: Static code block when parent-child class inherits, common code block, static method, construction method, etc.

Source: Internet
Author: User

Class B extends A, then the type a is the parent class with static code blocks, common code blocks, static methods, static member variables, ordinary member variables, common methods.
Subclasses are also like this, and then inherit the results about the printout of the program.
Involves the initialization order of various members of the Java class.
After testing, the following conclusions are obtained:

1. The parent class "static members" and "Static code blocks" are executed sequentially in the order in which they appear in the code.
2. Subclass "Static members" and "Static code blocks" are executed sequentially in the order in which they appear in the code.
3. The normal member variables of the parent class are assigned by ordinary member methods and ordinary code blocks, which are executed sequentially in the order in which they appear in the code.
4. Execute the constructor method of the parent class.
5. The "Normal member variables of the subclass are assigned by ordinary member methods" and "ordinary code blocks" are executed sequentially in the order in which they appear in the code.
6. Execute the constructor method of the subclass.


Here's a look at the test code and the graph of the test results:

Parent class:

Package com.lxk.classinittest;/** * Parent type * <p> * Created by lxk on 2017/4/20 */public class Parent {static {System.out . println ("Parent class: Static code block");} {SYSTEM.OUT.PRINTLN ("parent class: Normal code block");} private static string staticstringinparent = Initstaticstringinparent ();p rivate String stringinparent = Initstringinparent ();p ublic parent () {SYSTEM.OUT.PRINTLN ("Parent class: Constructor method");} private static String initstaticstringinparent () {System.out.println ("Parent class: Static method, called by static member variable assignment. "); return" Initstaticstringinparent ";} Private String initstringinparent () {System.out.println ("parent class: Normal member method, called by an ordinary member variable assignment. "); return" Initstringinparent ";}}

Sub-class:

Package com.lxk.classinittest;/** * Subclass * <p> * Created by lxk on 2017/4/20 */public class child extends Parent {priv Ate String Stringinchild = Initstringinchild ();p rivate static string staticstringinchild = Initstaticstringinchild (); {System.out.println ("Subclass: Normal code block");} static {System.out.println ("Subclass: Static code block");} Public Child () {System.out.println ("Subclass: Constructor Method");} private static String Initstaticstringinchild () {System.out.println ("Subclass: Static method, called by a static member variable assignment. "); return" Initstaticstringinchild ";} Private String Initstringinchild () {System.out.println ("Subclass: Normal member method, called by an ordinary member variable assignment. "); return" Initstringinchild ";}}

Test the Main method

Package com.lxk.classinittest;/** * Test members and initialization blocks (code block) initialization Order of Java classes * <p> * Created by lxk on 2017/4/20 */public class Cla ssinittest {public static void main (string[] args) {System.out.println ("test code Start"); new Child (); SYSTEM.OUT.PRINTLN ("Test Code End");}}


Result diagram of code execution:

To see the convenience, I put the code execution results, and get the theory put in a diagram.



Analysis:

In the subclass, I swapped the order of the Code, the order of the parent class and the subclass is not the same, look at the time note,

This is also a test of the conclusion of a link.

(Updated 2018.01.23)

There is a problem with the description above,

{SYSTEM.OUT.PRINTLN ("Parent class: Construct code block");}

This thing, it should be called, construct code blocks , construct the codes block, not what I wrote above.

Let's do a little more science here.
Common code blocks:
The {} that appears in a method or statement is called a normal code block.
Ordinary blocks of code and general execution order of statements are determined by the order in which they appear in the code-"first Occurrence first"
Static code block:
A block of code declared in Java using the static keyword .
Static blocks are used to initialize the class and initialize the properties of the class. Each static block of code is executed only once.
Because the JVM executes static blocks of code when the class is loaded, the static block of code executes before the main method.
If a class contains more than one static block of code, it will follow the code defined first, then execute after the definition.
Attention:
1 static code blocks cannot exist in any method body.
2 Static code blocks do not have direct access to static instance variables and instance methods and need to be accessed through instance objects of the class.
To construct a code block:
blocks of code that are defined directly in the class and do not have the static keyword are called {} construction blocks of code .
Construction blocks are called when the object is created, each time the object is created, and the order in which the code block is constructed takes precedence over the class constructor .
The execution order of this construction code block is not affected by the location of the method, I deliberately put him behind the constructor.



The various member initialization order of the Java class, such as: Static code block when parent-child class inherits, common code block, static method, construction method, and so on

Related Article

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.