When an object is created, all data members of the class where the object is located are initialized first. If the member variables in the object have objects, they will also perform initialization in order. After Initialization is complete, all Class Members call the constructor of the class where the object is located to create the object. Initialization is the role of the constructor.
Static objects (variables) are initialized before non-static objects. Static objects (variables) are initialized only once and will not be initialized when called again, but non-static objects must be initialized each time they are called.
Static variables of the main class in the program will be initialized before the main () method is executed.
Not only are 1st objects created, all static variables in the class should be initialized, but 1st static variables in the category class (no object is created, all static variables in this class should also be initialized in the order they are arranged in the class.
The initialization sequence includes the following:
1. The static members of the main class are initialized first.
2. the constructor of the superclasses of the main class is called in the order from the highest to the lowest.
3. Non-static object (variable) initialization of the main class.
4. Call the constructor of the main class.
Only one other constructor can be called once in a constructor. The statement that calls the constructor must be the first statement.
Class One
{
Public one (string Str)
{
System. Out. println (STR );
}
}
Class Two
{
One one_1 = new one ("one-1 ");
One one_2 = new one ("one-2 ");
Static one one_3 = new one ("one-3 ");
Public two (string Str)
{
System. Out. println (STR );
}
}
Public class test
{
Static Two two3 = new two ("two-3 ");
Public static void main (string [] ARGs)
{
System. Out. println ("test main () start ...");
Two two_1 = new two ("two-1 ");
System. Out. println ("------------");
Two two2 = new two ("two-2 ");
}
}
One-3
One-1
One-2
Two-3
Test main () start...
One-1
One-2
Two-1
-------------
One-1
One-2
Two-2