Java Static keyword, parent class subclass load Execution Order resolution

Source: Internet
Author: User
Many beginners may not understand the use of static methods, here I say my understanding:

    • The call to the static method does not depend on the creation class object

    • The This keyword is not used in the static method because no objects need to be created

    • Static member variables and static methods can only be called in static methods, because normal methods need to be called by creating objects, which conflicts with static methods

    • Static member variables and static methods can be called in normal methods and can be called directly from the class name. Static method

    • A static block of code that executes when the class is loaded and executes only once

The truth is over, look at a practical example:

Class person{    static{        System.out.println ("person static");    }    Public person (String str) {        System.out.println (' person ' +str);}    } public class Test {person person = new Person ("test");p ublic Test () {System.out.println ("test constructor");} static{        System.out.println ("Test static 1");    public static void Main (string[] args) {new MyClass ();} STATIC{SYSTEM.OUT.PRINTLN ("Test static 2");}}  Class MyClass extends Test {person person    = new Person ("MyClass");    static{        System.out.println ("MyClass Static");    }         Public MyClass () {        System.out.println ("MyClass constructor");}    }

First guess his output, then compare, see what's wrong, deepen understanding

Test static 1test static 2myclass Staticperson Staticperson testtest Constructorperson Myclassmyclass Constructor
    1. First load the test class, which contains two static blocks of code, then output test static 1,test static 2 in writing order

    2. In the main method, new has a MyClass, then loads the MyClass class, and the MyClass class also has a static code block, then the output MyClass static

    3. The MyClass class inherits from the test class, and the test class has already been loaded and no longer outputs the contents of the static code block

    4. After loading, the Test class starts executing, the person class is not loaded when the person is executed, and the person class is loaded with a static block of code, then output person static

    5. Executes the person's constructor, outputting the person Test

    6. Continue to test, enter the main method, new MyClass (), however MyClass inherits from the test class, first executes the test class construction method, output test constructor

    7. Continue execution Myclass,person person = new Person ("MyClass"), execute the Person class constructor method, output person MyClass

    8. Continue with the MyClass construction method, output MyClass constructor

    9. Execution complete

Based on these I summarize the following order of execution:

Static code Blocks---parent class construction Method--child class construction method

When the construction method and the new object exist at the same time, the new object's construction method is executed first. (Be careful not to create loop nesting, which causes memory overflow)

Related articles:

On the loading order of the parent class and subclass in Java

Java inheritance, whether the subclass inherits the constructor of the parent class

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.