Java member variable static variable code block static code fast loading sequence
The base class A Class B inherits Class A 1. When A new B instance is created, class loading is required first. (Classes are loaded by the java class loader only when they are created using the New call.) 2. When loading A class, first load parent class A and then the subclass B3, after the parent class A is loaded, complete static actions (including static code and variables, whose levels are the same, and the installation code is initialized in sequence) 4. After the Child class B is loaded, complete static class loading and start instantiation 1. when instantiating subclass B, first instantiate parent class A2. when instantiating parent class A, first instantiate members (non-static code) 3, constructor 4 of parent class A, member instantiation of subclass B (non-static code) 5, the constructor of subclass B First initializes the static code of the parent class ---> initializes the static code of the subclass --> initializes the non-static code of the parent class ---> initializes the parent class constructor ---> initializes the non-static code ---> test code for initializing subclass constructor:
Abstract class base {public int age = getNumber (100); static {System. out. println ("base static block");} {System. out. println ("base nonstatic block");} static int sage = getNumber (50); base () {System. out. println (age); System. out. println ("base start"); draw (); // The override method of the subclass is called. The value is 0! System. out. println ("base end");} static int getNumber (int base) {System. out. println ("base. getNumber int "+ base); return base;} public void draw () {System. out. println ("base. draw ") ;}} public class initializeOrder extends base {public int age = getNumber (1001); private int _ radius = getNumber (10); static int sage = getNumber (250 ); static {System. out. println ("subclass static block");} {System. out. println ("subclass nonstatic block");} initializeOrder (int radius) {_ radius = radius; System. out. println (age); draw (); // The value is 1000 System. out. println ("initializeOrder initialized");} public void draw () {System. out. println ("initializeOrder. draw "+ _ radius);} public static void main (String [] args) {// TODO Auto-generated method stub new initializeOrder (1000 );}}
Output: base static blockbase. getNumber int50base. getNumber int250subclass static blockbase. getNumber int100base nonstatic block100base base usage. draw 0 base endbase. getNumber int1001base. getNumber limit nonstatic limit. draw 1000 initializeOrder initialized