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)