A new object initialization

###############################

Today sum up, the initialization process new objects.

###############################

First, when free of static members , look at the map Case:

As shown, we first define the relevant class, Building, House (inherited Building), Villa (inherited House), at the same time, House members have variable LivingRoom, LivingRoom have member variables Bed and Desk.

Specific code to achieve the following:

 1 //建筑
 2 class Building {
 3     public Building() {
 4         System.out.println("Building");
 5     }
 6 }
 7 //房子
 8 class House extends Building{
 9     public LivingRoom livingRoom = new LivingRoom();//卧室
10     public House() {
11         System.out.println("House");
12     }
13 } 
14 //别墅
15 class Villa extends House{
16     public Villa () {
17         System.out.println("Villa");
18     }
19 }
20 //卧室
21 class LivingRoom {
22     public Bed bedFirst = new Bed();//
23     public Desk deskFirst = new Desk();//桌子
24     
25     public LivingRoom () {
26         System.out.println("LivingRoom");
27     }
28 }
29 //
30 class Bed {
31     public Bed () {
32         System.out.println("Bed");
33     }
34 }
35 //桌子
36 class Desk {
37     public Desk () {
38         System.out.println("Desk");
39     }
40 }
41 此时,我们new Villa():
42 public class InitializeDemo {
43     @Test
44     public void testInitialize() {
45          Villa Villa = new new Villa ();
 46 is      }
 47  }
 48  execution results:
 49  Building
 50  Bed
 51 is  Desk
 52 is  LivingRoom
 53 is  House
 54 is Villa

 

  In this case, when we create an object Villa, we will first try to create the object's parent class House of Villa, but the House also has a parent class Building, Building as well as the parent object. Therefore, in practice, this class will create object and initialize the object, the object is created and initialized object, the object is to create a building, building objects no member variables, so the direct implementation of the constructor, print out the building.

  Then, when the house began to create an object, the object has a member variable livingRoom house, therefore, will first initialize livingRoom, create livingRoom, will create the parent class object and initialize the object livingRoom not followed then create livingRoom instance, livingroom also has two members variable bed and desk, and thus, by definition, will order two member variables, has created bed and desk. After successful initialization of member variables, perform livingroom constructor, print livingroom. After a successful initialization livingroom, started house constructor, printing house.

  Finally, after the house initialization successful implementation of the Villa constructor, print out the Villa.

  Initialization successful.

Secondly, when a static member

. 1  class Family {
 2      Family (Class className) {
 . 3          System.out.println ( "Object Create Family" );
 . 4          System.out.println (className.getName ());
 . 5      }
 . 6  }
 . 7  class the Person {
 . 8      the Person ( ) {
 . 9          System.out.println ( "Create Person object" );
 10      }    
 . 11      public  static Family Family = new new Family (Person. class );
 12 is      static {
 13 is          System.out.println ( "static block of code executed Person" ) ;
14      }
 15      {
 16          System.out.println ( "Person execution code block" );
 . 17      }
 18 is  } 
 . 19  public  class Student the extends Person {    
 20 is  public Student () {
 21 is          System.out.println ( "Object Create student" );
 22 is      }
 23 is      {
 24          System.out.println ( "Student execution code block" );
 25      }
 26 is      static {
 27          System.out.println ( "static block of code execution Student" );
 28      }
 29     public  static Family staticFamily = new new Family (Student. class );
 30      @Test
 31 is      public  void Test () {
 32          System.out.println ( "=============" );
 33 is      }
 34 is      public Family Family = new new Family (Student. class );
 35  }
 36  The results:
 37  create objects Family
 38 is  com.tca.thinkInJava.chap7.Person
 39  performs Person static code block
 40  performs Student static code block
 41  creates an object Family
 42 com.tca.thinkInJava.chap7.Student
 43 is  executed Person block
 44  creates a Person object
 45  performing Student block
 46  creates an object Family
 47  com.tca.thinkInJava.chap7.Student
 48 creates student objects

 

  1. First @test in the implementation of the method, the student must create an instance of an object.

  2. To create an instance of an object student, the first step is to load the bytecode student parent class person and student class, and related static member variables are initialized, perform static code block.

  3. First, the parent loading bytecode class person, a static member variable public static family, family superior static block of code statement before, so the first family of static member variables are initialized. Create a family objects, print "objects to create family", "xxxxxx".

  4. After loading the bytecode person, and then loading the bytecode student. Static code block and then declare a static member before public static family staticfamily, we first perform a static code block, print "Performing student static code block", making the member variable is initialized to "Creating a family target", "xxxxxxxxxx".

  5. student create an instance of the parent class person, create an instance, the first non-static member variables and performs initialization code block, performing constructor method. So the first print "code block execution person," then print "Create a person object." Parent class object is created and initialized.

  6. Create Student subclass object, the first non-static member variables and performs initialization code block, the constructor is executed. Statement before the code block and then a non-static member variable declarations, so the first block of code, print "Performing student code block", and then the family is initialized, create a family objects, print "objects to create family", "xxxxxxxxxxx".

  7. Perform test testing methods.

 

to sum up:

  When free of static members , we new object A, first will first create an instance of the parent class B class A, class B if there are class C, the parent will first create an object of class C, and so on, it is the process of creating a recursive; when the class' parent class objects have all been created and initialized, the class will be created and initialized. However, when the class is initialized, it will first original member of the class object variable, and then execute the constructor of the class. Initialization and execution order of the code block member variables are determined by the declaration order, or perform the initialization according to the order, but are executed before the constructor method.

  When a static member , when the class is first loaded JVM, static member variables are initialized only once, static code block is executed only once. After the parent class will first load the byte code, and then load the subclass byte code, if there are instances of objects to create a class, and it is loaded in the bytecode parent class and the class of the class, will instantiate the class examples of the object and the object class instance of the parent class. Initialization and execution sequence static code block static member variable, is determined by the order of their declarations, according to the order or perform the initialization.

Guess you like

Origin www.cnblogs.com/HuiH/p/11670852.html