Class Bowl {Bowl (int Marker) {-------------------- 3, 5 Construction System.out.Println ("Bowl (" Marker ")" ); Void f (int Marker) {-------------------- 7, was called System.out.Println ("F (" Marker ")");}}
Class table {static bowl b1 = new bowl (1); ----------------- 2, before the construction, first static variable table () {------- ---------- 6, the variable has been completed, perform the constructor system.out.println ("Table ()"); b2.f (1); --------- -------- Call BOW F ()} Void F2 (Int Marker) {System.out.Println ("F2 (" Marker ")");} static bowl b2 = new bowl (2) ; ---------------- 4, the structure of the second static variable}
Class Cupboard {BOWL B3 = New Bowl (3); ------------------ 11, STATIC construction, construct non-Static member Static Bowl B4 = New Bowl (4 ); ----------------- 9, first static member construct Cupboard () {---------------- 12, members have constructed Ok, execute the constructor system.Print.Println ("Cupboard ()"); b4.f (2); -------------- 13, calling the f ()} void F3 (int Marker) {system.out.println ("F3 (" Marker ")") ")") ")") ")") ");} static bowl b5 = new bowl (5); ---------------- 10, construct static member} by sequence
Public class staticinitialization {public static void main (string [] args) {system.out.println ("Creating new cupboard () in main"); ----------- 14, members have constructed, Execute the main function body, first execute this sentence new curpboard (); ------------------ 15, temporary Cupboard object system.out.println ("CREATING New Cupboard () In main "); new cupboard (); t2.f2 (1); t3.f3 (1);} static table t2 = new table (); ---------------- --- 1, first static variable static cupboard t3 = new cupboard (); ----------------- 8, continue to construct Static member} Running results BOWL (1) Bowl 2) Table () f (1) Bowl (4) Bowl (5) Bowl (3) Cupboard () f (2) Creating New Cupboard () in Mainbowl (3) Cupboard () f (2) CREATING New Cupboard () In Mainbowl (3) Cupboard () F (2) F2 (1) F3 (1) ============================
## static type variable or method called class variable and class method class variables are initialized while being loaded into memory rather than STATIC variables are initialized when an instance is generated.
Because a Static method main () is to be performed, you must first load this class into memory and initialize the static variable.
So Static Bowl BL = New Bowl (1); to execute before the main function starts to execute
## Look at the class directory of the StaticInitialization.java, it will find that although compiled is just a .java file, but since the Java file declares 4 CLASS, it actually generated four Class files by this Java file. Below, you look at run StaticInitialization.class, at which time the system only loads this class, the other three classes have not been loaded, and there are two Static statements in StaticInitialization.class, so the system starts looking for the class specified by this two sentences. Table and Cupboard, found to start loading, so loaded, naturally, it will begin to initialize Static in these two classes.
## When the class is loaded into the JVM, the STATIC's code is executed once, and will not be executed later.
## This example reflects the difference between class variables and instance variables: class variables are "static" modifications before variables, no instance variables; class variables only once in each class, regardless of this class creation For example, the memory it uses is only assigned; the instance variable is assigned to each instance of the class. When the instance of the class is created, the system allocates memory for all instance variables in this class. In the above example, since B3 is an instance variable, a new B3 is created for each assignment, so its output is multiple times.
## Construct the class first construct the parent class (if the parent class has a parent class, first construct the parent class, the class push), first construct the STATIC variable (sequential in order), the construction is completed, construct After all of the STITIC variable variables, enter the constructor ## Static variable initialization is the instantaneous occurrence of the class by the initial call, and the non-static variable in the class is initialized before entering the constructor, remembers these two points. You can clearly understand that your output will always generate a static member when it comes to a class; then use the instance of this class, then call Initializer (default or human created); call the member; complete the creation. If you run a class, you will enter the main entry after initializing the static member, which does not create an instance of the class unless there is a new instance code in the implementation of the main implementation. ## JVM first construct a static object and then call main (), in the definition of Static, indicating that the static method is loaded when it is compiled. Later, it was a construction method.