Java Object creation Process
In some projects, a static block is used, and other objects are initialized in the constructor ... A deep understanding of the Java class initialization process, which will not be a problem.
What are the steps of the Java object?
Assuming that there is now a people class and no other classes are shown, the initialization process is like this.
1 The Java interpreter must look up the classpath to locate the People.class file. When the object is first created (the constructor is a static method), or when the static method/static domain of the People class is first accessed.
2 load People.class, all static initialization actions are performed, and static initialization occurs only once when the class object is first loaded.
3 When you create an object using New people (), you will first allocate enough storage space on the heap for the object.
4 Initialize the storage space to binary 0.
4 performs all initialization actions that appear at the property definition.
5 executes the constructor.
See an example:
Public class Bowl { Bowl (int marker) { System.out.println("Bowl (" + marker + ")"); } void F1 (int marker) { System.out.println ("F1 (" + marker + ")");} }
Public class Table { staticnew Bowl (1); Table () { System.out.println ("table ()"); BOWL2.F1 (1); } void F2 (int marker) { System.out.println ("F2 (" + marker + ")"); staticnew Bowl (2);}
Public class Cupboard { new Bowl (3); Static New Bowl (4); Cupboard () { System.out.println("cupboard ()"); BOWL4.F1 (2); } void f3 (int marker) { System.out.println ("F3 (" + Marker + ")"); staticnew Bowl (5);}
Public classstaticinitialization { Public Static voidMain (string[] args) {System.out.println ("Creating New Cupboard () in main"); Newcupboard (); System.out.println ("Creating New Cupboard () in main"); Newcupboard (); TABLE.F2 (1); CUPBOARD.F3 (1); } StaticTable Table =NewTable (); StaticCupboard cupboard =Newcupboard ();}
The results of the operation are as follows:
The initialization analysis is as follows:
Java Object creation Process