Static code block, non-static code block, execution sequence and number of Constructor

Source: Internet
Author: User
View code

 1   Package  Com. test;  2   3   Public   Class  Test1 {  4   5       /**  6   *  @ Param  ARGs  7       */  8       Public   Static   Void  Main (string [] ARGs ){  9           //  Todo auto-generated method stub  10 System. Out. println ("------------------ first instantiated object --------------------" );  11 Parent = New  Child ();  12 System. Out. println ("------------------ the second instantiation object --------------------" );  13 Parent = New  Child ();  14   }  15   }  16   17   Class  Parent {  18       Public   Static Staticvarible =New Staticvarible ("parent class-static variable 1" );  19       Public Staticvarible instvarible = New Staticvarible ("parent class-member variable 1" );  20   21       Static  {  22 System. Out. println ("parent class-static block" );  23   }  24  25   {  26 System. Out. println ("parent class-initialization block" );  27   }  28   29       Public  Parent (){  30 System. Out. println ("parent class-instance constructor" );  31   }  32   } 33   34   Class Child Extends  Parent {  35       Public   Static Staticvarible = New Staticvarible ("subclass-static variable 1" );  36       Public Staticvarible instvarible = New Staticvarible ("subclass-member variable 1" ); 37   38       Static  {  39 System. Out. println ("subclass-static block" );  40   }  41   42       Public  Child (){  43 System. Out. println ("subclass-instance constructor" );  44   } 45   46   {  47 System. Out. println ("subclass-initialization block" );  48   }  49   50   }  51   52   Class  Staticvarible {  53       Public Staticvarible (string info ){  54   System. Out. println (Info );  55   }  56 }

First instantiated object
Parent class-static variable 1
Parent class-static Block
Subclass-static variable 1
Subclass-static Block
Parent class-member variable 1
Parent class-initialization Block
Parent class-instance Constructor
Subclass-member variable 1
Subclass-initialization Block
Subclass-instance Constructor
------------------ The second instantiated object --------------------
Parent class-member variable 1
Parent class-initialization Block
Parent class-instance Constructor
Subclass-member variable 1
Subclass-initialization Block
Subclass-instance Constructor

 

Conclusion:

From the above results, we can see that when instantiating an object, the loading sequence of each part is as follows:

Parent class static member/parent class static initialization block-> subclass static member/subclass initialization block-> parent class member variable/parent class initialization block-> parent class constructor-> subclass Member variable/subclass initialization block-> subclass Constructor

StaticCodeBlock and static members are called only once when the class is loaded into the memory for the first time.

Non-static code blocks and members. Each time an object is created, it is called before the constructor.

Constructor, called at the end of each object creation.

Create a parent class object before creating a subclass object.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.