Interview questions--java static block, static variable loading sequence in detail __ questions

Source: Internet
Author: User
public class Test {//1. First step, prepare to load class public static void Main (string[) a                         RGS) {new Test ();                    4. The fourth step, a new class, but before new to process the anonymous code block} static int num = 4;
        2. The second step, static variables and static code block loading sequence from the written decision {num + + 3;           System.out.println ("B");                             5. The fifth step, in order to load the anonymous code block, the code block has the print} int a = 5;           6. Step sixth, load variable {//member variable ("C") in sequence (SYSTEM.OUT.PRINTLN);           7. The seventh step, in order to print the C} test () {//class constructor, fourth load System.out.println ("D"); 8. Step eight, finally load the constructor, complete the object's build} static {//3. The third step, static block, then executes the static code block, because there is output, so print a System
    . OUT.PRINTLN ("a");
    static void Run ()//static method, when called to load//note, E does not load {System.out.println ("E"); }
}

General Order: static block (static variable)--> member variable--> construction method--> static method
1, static code block (load only once) 2, construction method (to create an instance to load once) 3, static methods need to call to execute, so the final result is no E

public class Print {public

     print (String s) {
         System.out.print (S + "");
     }
 
public class parent{public

     static Print obj1 = new Print ("1");

     Public Print obj2 = new Print ("2");

     public static Print obj3 = new Print ("3");

     static{
         New Print ("4");
     }

     public static Print Obj4 = new Print ("5");

     Public Print obj5 = new Print ("6");

     Public Parent () {
         new Print ("7");
     }

 
public class child extends parent{

     static{
         new Print ("a");

     public static Print obj1 = new Print ("B");

     Public Print obj2 = new Print ("C");

     Public Child () {
         new Print ("D");
     }

     public static Print obj3 = new Print ("E");

     Public Print obj4 = new Print ("F");

     public static void Main (String [] args) {
         Parent obj1 = new Child ();
         Parent obj2 = new Child ();
     }
 

The output indicates that the program is executed in the following order:
If the class has not yet been loaded:
1. Initialize the static code blocks and static variables of the parent class, and the execution order of the static code blocks and static variables is only relevant to the order appearing in the code.
2, the execution subclass of static code block and static variable initialization.
3, the execution of the parent class instance variable initialization
4, the execution of the parent class constructor
5, the execution subclass of instance variable initialization
6, the execution subclass constructor

If the class has already been loaded:
static code blocks and static variables do not have to be repeated, and when the class object is created, only the variable initialization and construction methods associated with the instance are executed.

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.