Output order of static code blocks, non-static code blocks, constructors

Source: Internet
Author: User

Tag: Name color parses how many sys xtend results

code example:

Case one: When the parent class is not inherited

classHelloa { PublicHelloa () {System. out. println ("I ' M A class"); }    Static{System. out. println ("Static A"); //feature: Executes as the class is loaded and executes only once and takes precedence over the main function. Used to initialize a class. } {System. out. println ("A"); }     Public Static voidMain (string[] args) {NewHelloa (); NewHelloa (); }}

Output Result:

Static A
A
I ' M A class
A
I ' M A class

Analysis: The output order is: Static code block > Non-static code block > constructor, and no matter how many times the class static code block is executed only once, so the general situation of memory-intensive connection or other in the static code block, access speed, and only access once, save consumption.

Case two: When inheriting from a parent class

classHelloa extends hellob{ PublicHelloa () {System. out. println ("I ' M A class"); }    Static{System. out. println ("Static A"); } {System. out. println ("A"); }     Public Static voidMain (string[] args) {NewHelloa (); }}classHellob { PublicHellob () {System. out. println ("I ' M B class"); }    Static{System. out. println ("Static B"); } {System. out. println ("B"); }}

Operation Result:

Static B
Static A
B
I ' M B class
A
I ' M A class

Parsing: Output order: Parent class static code block > Subclass Static code block > parent non-static code block > Parent class constructor > subclass non-static code block > Subclass constructor;

Difference:
Static code blocks, which are loaded and executed once when the virtual machine loads the class;
A non-static block of code that executes when the object is created (that is, when the object is new), and every time the object is created.

Analysis: The initialization order of the object: first, the static content of the parent class is executed, the static content of the parent class executes, then the static content of the subclass is executed, and when the static content of the subclass is executed, the parent class has no non-static code block, and if there is a non-static block of code for the parent class , then executes the parent class's construction method, and after the parent class's construction method executes, it goes on to see if the subclass has no non-static code block, and if there is one, executes the subclass's non-static code block. The non-static code block of the subclass executes and then executes the subclass's construction method. In short, the static code block content executes first, then executes the parent class non-static code block and construction method, then executes the subclass non-static code block and constructs the method.
and the construction method of the subclass, regardless of the constructor method with no parameters, the default it will first look for the parent class without parameters of the construction method. If the parent class does not have a constructor with no arguments, the subclass must use the supper key to call the parent class with the constructor of the parameter, otherwise the compilation cannot pass.

Output order of static code blocks, non-static code blocks, constructors

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.