The constructor cannot stop automatic initialization. It will occur before the constructor is called.
Class counter {
Int I;
Counter () {I = 7 ;}
}
I is first set to 0 and then changed to 7. this is true for all basic types and object references, including variables with initial values specified during definition. therefore, the compiler does not force you to initialize the elements in the constructor or before using them.
Initialization sequence: within the class, the sequence of variable definitions determines the initialization sequence. even if the variable definitions are scattered between the method definitions, they are still initialized before any methods (including constructors) are called.
Static Data initialization: the situation is not different. note: The static object is initialized only when the first object is created (or accessed for the first time. after that, the static object will not be initialized again.
The initialization sequence is to first "static" objects (if they are not initialized due to the previous object creation process), and then "non-static" objects, execute the constructor again (in this case, many actions may be involved, especially when inheritance is involved ).
Class bowl ...{
Bowl (INT marker )...{
System. Out. println ("bowl (" + marker + ")");
}
Void F (INT marker )...{
System. Out. println ("F (" + marker + ")");
}
}
Class table ...{
Static bowl b1 = new bowl (1 );
Table ()...{
System. Out. println ("table ()");
B2.f (1 );
}
Void F2 (INT marker )...{
System. Out. println ("f2 (" + marker + ")");
}
Static bowl b2 = new bowl (2 );
}
Class cupboard ...{
Bowl B3 = new bowl (3 );
Static bowl B4 = new bowl (4 );
Cupboard ()...{
System. Out. println ("cupboard ()");
B4.f (2 );
}
Void F3 (INT marker )...{
System. Out. println ("F3 (" + marker + ")");
}
Static bowl B5 = new bowl (5 );
}
/** // * Cupboard class execution sequence: 1. static member, 2. Initialization of non-static variables or Object Instantiation, 3. Others (understandable according to running results )*/
Class staticinitialization ...{
Public static void main (string [] ARGs )...{
System. Out. println ("creating new cupboard () in main ");
New cupboard ();
System. Out. println ("creating new cupboard () in main ");
New cupboard ();
T2.f2 (1 );
T3.f3 (1 );
}
Static table t2 = new table ();
Static cupboard T3 = new cupboard ();
}
Java allows multiple static initialization actions to be organized into a special "static block ". like other static initialization actions, when the class is loaded, the "static block" is executed and the code is only executed once.
When other classes are used in a program, classes are loaded when they are used for the first time, rather than loading all the classes that may be used in the program at program startup.
Whether the execution (1) code accesses the static C1 object or (2) the code accesses the static C1 object, the static initialization action of cups will be executed (Label (2) it does not matter whether the code is activated for one or two lines, because the static Initialization is only performed once ).
Similar syntaxes are also available in Java to initialize non-static variables of each object (you only need to remove the static variables before the "static block ).
Class cup ...{
Cup (INT marker )...{
System. Out. println ("cup (" + marker + ");");
}
Void F (INT marker )...{
System. Out. println ("F (" + marker + ");");
}
}
Class cups ...{
Static cup C1;
Static cup C2;
Static ...{
C1 = new cup (1 );
C2 = new cup (2 );
System. Out. println ("static code block ");
}
Cups ()...{
System. Out. println ("Cups ");
}
}
Class explicitstatic ...{
Public static void main (string [] ARGs )...{
System. Out. println ("Inside main ()");
// Cups. c1.f (99); (1)
}
// Static cups x = new cups (); (2)
// Static cups y = new cups ();
}