A quick understanding of static code blocks, constructor blocks, and constructors in java classes

Source: Internet
Author: User

Public class testStatic extends Fa {static {System. out. println ("son: static code block") ;}{ System. out. println ("son: constructing code blocks");} public testStatic () {System. out. println ("son: constructor block");} public testStatic (String s) {super (s); System. out. println ("son: constructor block" + s);} public static void main (String [] args) {testStatic te; System. out. println (); Fa t = new testStatic ("ai") ;}} class Fa {static {System. out. println ("fa: static code block") ;}{ System. out. println ("fa: constructing code blocks");} public Fa () {System. out. println ("fa: constructor block");} public Fa (String s) {System. out. println ("fa: constructor block" + s );}}

 


Output result: ================== fa: static code block son: static code block fa: constructor block aison: constructor: constructor block ai ==================== draw a conclusion: 1. execution sequence: static code> code block> constructor 2. the static code block is executed with the class declaration, while the Construction Code block and construction method are executed after a class is instantiated. The construction code block takes precedence over the construction method 3. when the subclass testStatic does not use super (s), the output is the fa: constructor block by default, which calls the non-parameter constructor of fa.

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.