I believe that when we interview Java there will always be some companies to do written questions, and Java class loading and object creation process of knowledge points is also one of the common topics. The following is an example of a detailed analysis:
package com.test;
public class Parent {
int a = ten;
static int b = 11;
//Static code block
Static {
System.out.println ("Parent Static code block: b=" + b);
b++;
}
//code block
{
System.out.println ("Parent code block: a=" + a);
SYSTEM.OUT.PRINTLN ("Parent code block: b=" + b);
b++;
a++;
}
//parameterless constructor
Parent () {
System.out.println ("parent no parameter constructor: a=" + a);
SYSTEM.OUT.PRINTLN ("Parent no parameter constructor: b=" + b);
}
//Parameter constructor
parent (int a) {
System.out.println ("parent has parameter constructor: a=" + a);
SYSTEM.OUT.PRINTLN ("Parent has a parameter constructor: b=" + b);
}
//Method
Void function () {
System.out.println ("Parent function Run ...");
}
}
package com.test;
public class child extends Parent {
int x = ten;
static int y = 11;
//Static code block
Static {
System.out.println ("Child Static code block: y=" + y);
y++;
}
//code block
{
System.out.println ("Child code block: x=" + x);
SYSTEM.OUT.PRINTLN ("Child code block: y=" + y);
y++;
X + +;
}
//constructor
Child () {
System.out.println ("Child no parameter constructor: X" + x);
SYSTEM.OUT.PRINTLN ("Child no parameter constructor: y=" + y);
}
//Method
Void function () {
System.out.println ("Child function Run ...");
}
}
Package com.test;
public class Testpc {
public static void Main (string[] args) {
Child demo = new Child ();
Demo.function ();
System.out.println ("...... ..... ..... ..... ..... ..... ..... ..... ..... ..... ..... ..... ..... ..... ......
/* Child child = new Child ();
Child.function (); */
}
}
Results:
Parent Static code block: b=11
Child Static code block: y=11
Parent code block: a=10
Parent code block: b=12
Parent non-parametric constructor: a=11
Parent non-parametric constructor: b=13
Child code block: x=10
Child code block: y=12
Child non-parametric constructor: X11
Child non-parametric constructor: y=13
Child function Run ...
............................................................
Parent code block: a=10
Parent code block: b=13
Parent non-parametric constructor: a=11
Parent non-parametric constructor: b=14
Child code block: x=10
Child code block: y=13
Child non-parametric constructor: X11
Child non-parametric constructor: y=14
Child function Run ...
Detailed analysis of the loading and object creation processes of Java classes