Initialization order of classes in Java

Source: Internet
Author: User


Test code:

1 Public classTest022 {3 PublicTest02 ()4 {5System.out.println ("Test02");6 }7 Public Static voidMain (string[] args)8 {9Test03 test03=NewTest03 ();//1Ten //test03.f (); //2 One //System.out.println (test03.i);//3 A } - } - the classTest03 - { - Static inti = 3; - StaticTest02 test02 =NewTest02 (); +Test01 test01 =NewTest01 (); - + Static A { atSystem.out.println ("Static"); - } - { -System.out.println ("Nostatic"); - } - Static voidf () in { -System.out.println ("F ()"); to } + - PublicTest03 () the { *System.out.println ("Test03"); $ }Panax Notoginseng}
The result of the execution of 1 is as follows: test02statictest01nostatictest03 execution 2 results as follows: TEST02STATICF () executes 3 results as follows: TEST02STATIC3    The conclusions are as follows: 1. When you create an object, you initialize the class where the object resides, in the order of static objects > static code blocks > Generic Objects > Common code blocks > Construction Method 2. When you simply invoke a static or static method of a class, only the static behavior is initialized at this point: Static object > Static code block > static variable (static method)

Initialization order of classes in Java

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.