java--Common code block static code block execution order

Source: Internet
Author: User

1 classB {2 3      PublicB () {4         Super();5System.out.println ("Constructor B");6     }7     {8System.out.println ("Normal code block B");9     }Ten     Static{ OneSystem.out.println ("Static code block B"); A     } -      - } the  Public classClassAextendsB { -  -      PublicClassA () { -         Super(); +System.out.println ("Constructor a"); -     } +     { ASystem.out.println ("Normal code block a"); at     } -     Static{ -System.out.println ("Static code block a"); -     } -      Public Static voidMain (string[] args) { -ClassA a=NewClassA (); in     } -}

The results of the operation are as follows:

Static code block B
Static code block A
Common code block B
Constructor b
Common code block A
Constructor a

can see The static code block is executed first, then the normal code block of the parent class, the parent class constructor, and then the generic code block of the subclass, the subclass constructor

java--Common code block static code block execution order

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.