A review of the execution order of static blocks, main methods, construction blocks, and construction methods in Java

Source: Internet
Author: User
write this article to review the Java static block, Main method, building blocks, construction methods of the order of execution, directly run the following code can be at a glance, no more said.
/** * Run the Parent class Main method The order of execution of each block scope: * Static block (and only once)-> Main method-> construction method of building block->/public class Parent {privat
    e int age = 0;
    Private String name = NULL;

    private static String company = NULL;
        static {System.out.println ("Parent static Block runing");
    Company = "Parent-ahik";
        {System.out.println ("Parent constructor block runing");
    THIS.name = "parent";
        Public parent () {System.out.println ("Parent construct method parent () runing");
    This.age = 28;
        Public parent (int age) {System.out.println ("Parent construct method parent (int age) runing");
    This.age = age;
                @Override public String toString () {return ' parent{' + ' age= ' + Age +
    ", Name= '" + name + ' \ ' + ", company= '" + Company + "'" + '} '; public static void Main (String ... args) {System.out.println ("Parent main")Runing ");
        Parent P1 = new parent ();
        System.out.println (p1);
        Parent P2 = new parent (33);

        System.out.println (p2);
            {int i = 100;
        System.out.println (i);
        int i = 99;
    System.out.println (i); }}///////////////////////////////////////////////////////////////** * Run Subclass Main Method The order of execution of individual block scopes: * Parent class static block (and only execute once -> * Subclass static block and execute only once)-> * Subclass Main Method-> * Parent class building Block-> * Parent class construction Method-> * Subclass Construction Block-> * Subclass Construction Method/Public
    Class Child extends Parent {private int age = 0;
    Private String name = NULL;

    private static String company = NULL;
        static {System.out.println ("Child static Block runing");
    Company = "Child-ahik";
        {this.name = "child";
    SYSTEM.OUT.PRINTLN ("Child construct block start runing");
        Public Child () {super (66);
        This.age = 99; SYSTEM.OUT.PRINTLN ("Child construct Method child () runing");
        (int age) {Super (77);
        This.age = age;
    SYSTEM.OUT.PRINTLN ("Child construct (int age) runing");
        public static void Main (String ... args) {System.out.println ("Child main runing");
        Child C1 = new Child ();
        SYSTEM.OUT.PRINTLN (C1);
        Child C2 = new Child (100);
    System.out.println (C2);




 }

}
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.