Java initialization sequence

No inheritance initialization sequence

 

class  the Sample 
{ 
      the Sample (String S) 
      { 
            System.out.println (S); 
      } 
      the Sample () 
      { 
            System.out.println ( "default constructor is called the Sample" ); 
      } 
} 
class  the Test {
       static  the Sample SAM = new new  the Sample ( "static member sam initialization" ); 
      the Sample SAM1 = new new  the Sample ( "SAM1 member initialization" );
       static { 
            System.out.println ( "static block execution" );
             IF (sam == null ) System.out.println ( "sam is null"); 
            Sam = new new  the Sample ( "block to initialize a static member variable sam" ); 
            } 
      the Test () 
      { 
            System.out.println ( "default constructor is called the Test" ); 
      } 
 
} 
// main function 
 public  static  void   main ( STR String []) 
{ 
 
     the Test A = new new  the Test (); 
 
 }

The output is:

Sam initialize static member static member initialization -----

performing static block static block is executed -----

Sam static initialization block within a static member variables ---- block execution

sam1 member initialization ----- ordinary member initialization

It can be concluded:

First a static member variable initialization (Note, Static can be seen as a static member, and its related execution order in the class stated in the order)

b ordinary member initialization

c constructor is executed.

For static members (static blocks may be viewed as a normal static member, which is not necessarily performed first in the case of initialization) and general members, which only its initialization sequence in the order defined in the class concerned, and other factors unrelated.

Test the default constructor is called constructor executes -----

 

class  the Test {
       static { 
            System.out.println ( "1 performs static block" ); 
            } 
      static  the Sample staticSam1 = new new  the Sample ( "static member staticSam1 initialization" ); 
      the Sample SAM1 = new new  the Sample ( "SAM1 member initialization" );
       static  the Sample = staticSam2 new new  the Sample ( "static member staticSam2 initialization" );
       static { 
            System.out.println ( "static execution block 2" ); 
            } 
      the Test () 
      { 
            System.out.println ( "default constructor is called the Test" );
      } 
      The Sample SAM2 = new new  the Sample ( "SAM2 member initialization" ); 
 
}

The output is:

1 performs static block

Static member initialization staticSam1

Static member initialization staticSam2

2 static block static members executed -----

sam1 member initialization ----- ordinary members

sam2 member initialization                

Test the default constructor is called constructor -----

 

Initialization sequence in succession

 

 

class Test{
      static{
            System.out.println("父类static 块 1  执行");
            }
      static Sample staticSam1=new Sample("父类 静态成员staticSam1初始化");
      Sample sam1=new Sample("父类 sam1成员初始化");
      static Sample staticSam2=new Sample("父类 静态成员staticSam2初始化");
      static{
            System.out.println("父类 static 块 2  执行");
            }
      Test()
      {
            System.out.println("父类 Test默认构造函数被调用");
      }
      Sample sam2=new Sample("父类 sam2成员初始化");
 
}
 
class TestSub extends Test
{
      static Sample staticSamSub=new Sample("子类 静态成员staticSamSub初始化");
      TestSub()
      {
            System.out.println("子类 TestSub 默认构造函数被调用");
      }
      Sample sam1=new Sample("子类 sam1成员初始化");
      static Sample staticSamSub1=new Sample("子类 静态成员staticSamSub1初始化");
      
      static{System.out.println("子类 static 块  执行");}
      Sample sam2=new Sample("子类 sam2成员初始化");
}

 

 

 

输出结果为:

父类 static 块 1  执行

父类 静态成员staticSam1初始化

父类 静态成员staticSam2初始化

父类 static 块 2  执行

                        --------父类静态成员初始化

子类 静态成员staticSamSub初始化

子类 静态成员staticSamSub1初始化

子类 static 块  执行

                        -------子类静态成员初始化

父类 sam1成员初始化

父类 sam2成员初始化

父类 Test默认构造函数被调用       

                        -------父类普通成员初始化和构造函数执行

子类 sam1成员初始化

子类 sam2成员初始化

子类 TestSub 默认构造函数被调用

                        -------父类普通成员初始化和构造函数执行

由此得出Java初始化顺序结论:

1 继承体系的所有静态成员初始化(先父类,后子类)

2 父类初始化完成(普通成员的初始化-->构造函数的调用)

3 子类初始化(普通成员-->构造函数)

注意:java初始化顺序。初始化子类必先初始化父类。子类的构造方法会隐式去调用父类无参的构造方法(不会在代码中显示)。但如果父类没有无参的构造方法,就必须在子类构造方法第一行显示调用父类的有参构造方法,否则编译失败!!!

 

转载于:https://www.cnblogs.com/lucky1024/p/11046347.html

Guess you like

Origin blog.csdn.net/weixin_34167819/article/details/93249930