Java-class life cycle (ii)

Source: Internet
Author: User

The java-class life cycle (a) is described above, and the theoretical comparison is stronger. Think too complicated classmate, glanced at the example of this article to deepen understanding.

Give the question first, and see if it is correct.


/** * Class Load Test base class *  * @author Peter_wang * @create-time 2014-5-14 morning 8:41:23 */public class Superclass {    static {        S Ystem.out.println ("Call super Static method!");    }    Private Superhelpclass Msupersuperclass = new Superhelpclass ();    Public superclass () {        System.out.println ("Call Super constructor");    }    Public superclass (Int. flag) {        System.out.println ("Call Super constructor" + flag);}    }

/** * Class Load Test sub Class *  * @author Peter_wang * @create-time 2014-5-14 morning 8:43:07 */public class subclass extends    superclass {    private Superclass Msuperclassfirst = new superclass (1);    Public subclass () {        System.out.println ("Call Sub constructor");    }    /**     * @param args */public    static void Main (string[] args) {        Subclass subclass = new Subclass ();    }    static {        System.out.println ("Call sub static method!");    }    private static Superclass Msuperclasssecond = new Superclass (2);}

To run the results.


Principle Analysis:

1. Start the JVM and run the subclass Portal Main method. The accidental discovery class was not loaded, and the. class file was loaded with ClassLoader .

2. After loading the subclass, run the static block before running main, then run the static domain

3. Parent class takes precedence over subclass run

4. Run the new subclass () in the main method, run the parent class to initialize all variables, and then run the parent class constructor

5. Run the subclass all variable initialization, and finally run the subclass constructor


Operation Result:

Call Super static Method!call sub static Method!call Super constructor 2call Super Constructorcall Super constructor 1call Sub constructor


Java-class life cycle (ii)

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.