Examples of java

Initialization block

  • In fact, the initialization is a block artifact, after use javac over java class initialization block disappears, initialization code is restored to the front of each code constructor
  • In the example of the process of: initializing the preceding block to execute, after subsequent executions
  • Modifiers can only be static initialization block
  • Responsible for implementing the common initialization block initializes the object responsible for performing static initialization block initialization class
  • Implicit initialization block only executed when creating java object, performed before the constructor

When you create a java object initialization sequence is :

1, the first class is loaded:

  • To perform static initialization code top-level parent class, then drill down static perform an indirect parent class initialization code,
    and finally the direct parent perform static initialization code;

  • Assigned static member variable and static initialization block is declared initial value to a static class initialization code,
    their execution order of the source code written in the same order.

2, then began to object instantiation

  • First initial value specified when performing initialization block declaration or instance variables (both the source and the execution order written in the same order),
  • Then execute code in the constructor.

example:

. 1  class Root {
 2      static {
 . 3          System.out.println ( "Root static initialization block" );
 . 4      }
 . 5      
. 6      {
 . 7          System.out.println ( "normal initialization block the Root" );
 . 8      }
 . 9      
10      public Root () {
 . 11          System.out.println ( "Root no arguments constructor" );
 12 is      }
 13 is      
14  }
 15  
16  class Mid the extends Root {
 . 17      static {
 18 is          System.out.println ( "static initialization block Mid");
 . 19      }
 20 is      
21 is      {
 22 is          System.out.println ( "Mid general initialization block" );
 23 is      }
 24      
25      public Mid () {
 26 is          System.out.println ( "Mid no arguments constructor" );
 27      }
 28      
29      public Mid (String MSG) {
 30          the this ();
 31 is          System.out.println ( "Mid the parameterized constructor, which parameter values:" + MSG);
 32      }
 33 is      
34 is  }
 35  
36  class Leaf the extends Mid {
 37 [      static{
 38 is          System.out.println ( "Leaf static initialization block" );
 39      }
 40      
41 is      {
 42 is          System.out.println ( "Leaf normal initialization block" );
 43 is      }
 44 is      
45      public Leaf () {
 46 is          Super ( "crazy Java" );
 47          System.out.println ( "Leaf no arguments constructor" );
 48      }
 49  }
 50  
51 is  public  class the Test {
 52 is      public  static  void main (String [] args) {
 53 is          new new Leaf () ;
 54         new Leaf();
55     }
56 }

Results of the:

Static initialization block of Root 
Mid static initialization block 
Leaf static initialization block 
normal initialization block Root of 
non Root reference constructor 
Mid common initialization block 
Mid no arguments constructor 
Leaf common initialization block 
Leaf no arguments constructor 
the Root Common initialization block 
Root no arguments constructor 
normal initialization block of Mid 
Mid no arguments constructor 
normal initialization block of Leaf 
Leaf no arguments constructor

Program execution flow:
  Root static initialization block ->

  Mid static initialization block ->

  Leaf static initialization block ->

  Back to the Test class new Leaf () ->

  Leaf enters no-argument constructor  ->

  There are super into the Mid-argument constructor ->

  this no-argument constructor into the MId ->

  super into the Root constructor with no arguments ->

  Root initialization block into the execute printing ->

  Super entering the no-argument constructor and execute print ->

  Mid initialization block into the normal printing is executed ->

  Enter MId no-argument constructor and execute print ->

  Mid has entered parameters configured to execute printing ->
  into the initialization block print Leaf ->

  Enter the no-argument constructor Leaf Print -> .........

Guess you like

Origin www.cnblogs.com/haiyuexiaozu/p/10984396.html