The static block and static variable loading sequence in the interview question--java

Source: Internet
Author: User

1  Public classTest {//1. First step, prepare to load class2 3      Public Static voidMain (string[] args) {4         NewTest ();//4. Fourth step, new class, but to process anonymous code blocks before new5     }6 7     Static intnum = 4;//2. Second step, the loading order of static variables and static code blocks is determined by the writing sequence.8 9     {Tennum + = 3; OneSystem.out.println ("B");//5. Fifth step, the anonymous code block is loaded in sequence, the code block has a print A     } -  -     intA = 5;//6. Sixth step, load variables in sequence the  -{//member Variable Third -System.out.println ("C");//7. Seventh Step, print C in sequence -     } +  -Test () {//constructor of the class, fourth load +System.out.println ("D");//8. The first step, the final load of the constructor, complete the creation of the object A     } at  -     Static{//3. Third step, static block, then execute static code block, because there is output, so print a -System.out.println ("a"); -     } -  -     Static voidRun ()//static methods, which are only loaded when called//Note that e is not loaded in     { -System.out.println ("E"); to     } +}

Output: ABCD

General Order: Static blocks (static variables)---member variables---code blocks---construction methods--Static methods
1, Static code block (only load once) 2, construction method (to create an instance to load once) 3, the static method needs to be called to execute, so the final result is not e

1  Public class Print {23       Public Print (String s) {4          System.out.print (S + ""); 5      }6  }
1  Public classparent{2 3       Public StaticPrint obj1 =NewPrint ("1");4 5       PublicPrint Obj2 =NewPrint ("2");6 7       Public StaticPrint obj3 =NewPrint ("3");8 9      Static{Ten          NewPrint ("4"); One      } A  -       Public StaticPrint Obj4 =NewPrint ("5"); -  the       PublicPrint obj5 =NewPrint ("6"); -  -       PublicParent () { -          NewPrint ("7"); +      } -  +}
1  Public classChildextendsparent{2 3      Static{4          NewPrint ("a");5      }6 7       Public StaticPrint obj1 =NewPrint ("B");8 9       PublicPrint Obj2 =NewPrint ("C");Ten  One       PublicChild () { A          NewPrint ("D"); -      } -  the       Public StaticPrint obj3 =NewPrint ("E"); -  -       PublicPrint Obj4 =NewPrint ("F"); -  +       Public Static voidmain (String [] args) { -Parent obj1 =NewChild (); +Parent Obj2 =NewChild (); A      } at}
 Execute main method, the program output order is:  1  < Span style= "font-size:16px" >3  4  5   A b e  2  6  7 C F D  2  6  7 C F D       

The output shows that the order of execution of the program is:
If the class has not yet been loaded:
1, the static code block and static variable initialization of the parent class are executed first, and the execution order of static code blocks and static variables is only related to the order in which the code appears.
2, the execution of the subclass static code block and static variable initialization.
3. Perform instance variable initialization of parent class
4. Execute the constructor of the parent class
5. Initialization of instance variables for subclasses
6. Execute the subclass constructor

If the class has already been loaded:
Static code blocks and static variables do not have to be repeated, and when you create a class object, only the instance-dependent variable initialization and construction methods are performed.

The loading sequence of static blocks and static variables in the question--java of (turn) faces

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.