The Java code looks like this:
Class Name {
Name () {
Screen.print ("8", " Name instance constructor!");
}
Name (String value) {
Screen.print ("9", " Name instance constructor with: " + value);
}
static {
Screen.print ("10", " Name static init block");
}
}
Class Person {
Private name name = new name ();
static {
Screen.print ("5", "erson static init block!");
}
Person () {
Screen.print ("6", " person instance constructor!");
}
{
Screen.print ("7", " Person instance init block!");
}
}
public class Customer extends person {
Static name name = new name (" tom");
static {
Screen.print ("2", " Customer static init block!");
}
{
Screen.print ("3", " Customer instance init block!");
}
Customer () {
Screen.print ("4", " Customer instance constructor!");
}
public static void Main (string[] args) {
Screen.print ("1", " Customer Initial test!");
Person p = new Customer ();
}
}
Class Screen {
static void print (String sn, string msg) {
System.out.println (" p[" + sn + "]-" + msg);
}
}
Can you just tell me what the results are?
The output of the execution result is:
P[5]-person static init block!
P[10]-Name static init block
P[9]-Name instance constructor with:tom
P[2]-Customer static init block!
P[1]-Customer Initial test!
P[8]-Name instance constructor!
P[7]-person instance init block!
P[6]-person instance constructor!
P[3]-Customer instance init block!
P[4]-Customer instance constructor!
Are you right to answer?
Why is it?
The explanations are as follows:
Java Code {screen.print ("7", " Person instance init block!"); }
Called Instance initialization block
Java Code static{screen.print ("2", " Customer static init block!"); }
Called Static initialization block
The class load priority order is:
Static zone (static initialization block and class static instance variable) > Non-static initialization area of a class (instance initialization block and instance variable) > Class constructors
The static zone is loaded the first time it encounters the class, and non-static zones and constructors are loaded at the Tianjin website when the object is created (both new). Multiple static zones of the same class are loaded in the order in which the code appears, as is the non-static zone. Static is loaded only once, for all classes, not for class objects.
In this program:
1. Enter the customer class, check that there is a extends, there is a parent class person.
2. Enter the person class and check that no parent class exists, but there is a static zone, execute output "5". The non-static and person constructors are not loaded because the person is not created.
3. Enter the Customer class program body and discover that there are multiple static zones.
The first is static name name = new name (" tom"). Create name, enter the name class, load name static initialization fast, output "10". No more than the static zone, load the corresponding constructor, output "9".
The second occurrence of the static is "2"
4. Go to main thread, run the first line of output "1"
5. Create a non-parametric customer () object. Because the person class is its parent class, when you create a subclass, the system will automatically create a parent class object.
Enter the person class again, in order to create the name object first. Entering the name class, its static zone has been loaded and does not need to be loaded. No non-static zone. Call the appropriate constructor, output "8", and exit the name class. Load the person class with the second non-static zone, output "7", and then load its constructor output "6"
Enter the customer class, load the initialization block output "3", and then the constructor output "4"
6. Exit the main thread
Test whether you really understand the Java runtime and the Order of execution