Java after-school learning

  Why lower error codes (new Foo ())?

 1 public class Practice {
 2     public static void main(String[] args) {
 3         Foo obj1 = new Foo();
 4     }
 5 }
 6 class Foo{
 7     int value;
 8     public Foo(int initValue) {
 9         value = initValue;
10     }
11 }

Reason: when the class provides a method of constructing custom, will cause the system to not provide a default constructor.

  Run of the code:

. 1  class Root
 2  {
 . 3      static {
 . 4          System.out.println ( "Root static initialization block" );
 . 5      }
 . 6      {
 . 7          System.out.println ( "normal initialization block the Root" );
 . 8      }
 . 9      public Root ( )
 10      {
 . 11          System.out.println ( "Root no argument constructor" );
 12 is      }
 13 is  }
 14  class Mid the extends Root
 15  {
 16      static {
 . 17         System.out.println ( "Mid static initialization block" );
 18 is      }
 . 19      {
 20 is          System.out.println ( "Mid general initialization block" );
 21 is      }
 22 is      public Mid ()
 23 is      {
 24          System.out.println ( "Mid zero-argument constructor" );
 25      }
 26 is      public Mid (String MSG)
 27      {
 28          // by calling this same class constructor overloads 
29          this ();
 30          System.out.println ( " Mid constructor parameters with which the parameter values: "+ MSG);
 31 is      }
 32  }
 33 is class Leaf the extends Mid
 34 is  {
 35      static {
 36          System.out.println ( "Leaf static initialization block" );
 37 [      }
 38 is      {
 39          System.out.println ( "Leaf normal initialization block" );
 40      }    
 41 is      public Leaf ()
 42      {
 43          // by calling the parent class super has a string constructor parameter 
44 is          super ( "the Java presentation initialization sequence" );
 45          System.out.println ( "Leaf performed constructor" );
 46      }
 47  
48  }
 49  
50 public class TestStaticInitializeBlock
51 {
52     public static void main(String[] args) 
53     {
54         new Leaf();
55         
56 
57     }
58 }

Static initialization block execution order:

First runtime execution static, i.e. static initialization block, the order is started by the parent, then execute subclass; After first started Common initialization block by the parent, then perform constructor with no arguments, the final implementation has parameters configured after the method, the parent class subclass begin executing the order unchanged.

Guess you like

Origin www.cnblogs.com/20183711PYD/p/11691605.html