The rules for initializing Java fields:
1 with actual parameter values, initialize as actual parameter values such as obj=new Init (300);
System. out. println (Obj.field);
2 when no actual parameter value is given, find the default constructor such as Init obj=new init ();
System. out. println (Obj.field);
2 when there is an inheritance relationship between multiple classes, creating a subclass object causes the parent class to initialize the fast executing program:
Package com;
class Father {
Static
{
System. out. println ("Parent class static Block");
}
Public Father () {
System. out. println ("Parent class construction method");
}
}
class Child extends Father {
Static
{
System. out. println ("Subclass static Block");
}
Public Child () {
System. out. println ("Sub-Class construction method");
}
}
Public class Duo {
Public Static void Main (String []args) {
New Father ();
System. out. println ("--------------");
New Child ();
}
}
3 Summarize the execution order of static initialization blocks:
The code is as follows: Package com;
class Root
{
Static {
System. out. println ("Static initialization block of Root");
}
{
System. out. println ("Root's ordinary initialization block");
}
Public Root ()
{
System. out. println ("Root parameter-free constructor");
}
}
class Mid extends Root
{
Static {
System. out. println ("Static initialization block for mid");
}
{
System. out. println ("normal initialization block for mid");
}
Public Mid ()
{
System. out. println ("parameter-free constructor for mid");
}
Public Mid (String msg)
{
Calling overloaded constructors in the same class through this
This ();
System. out. println ("Mid with parametric constructor, its parameter value:" + msg);
}
}
class Leaf extends Mid
{
Static {
System. out. println ("Static initialization block of the Leaf");
}
{
System. out. println ("ordinary initialization block of the Leaf");
}
Public Leaf ()
{
A constructor that invokes a string argument in the parent class through Super
Super ("Java Initialization sequence demo");
System. out. println ("The constructor that executes the leaf");
}
}
Public class Q
{
Public Static void Main (string[] args)
{
New Leaf ();
}
}
The analysis is as follows: First, the static content of the parent class is executed, the static content of the parent class executes, then the static content of the subclass is executed, and when the static content of the subclass is executed, the parent class has no non-static code block. If there is a non-static block of code that executes the parent class, the non-static code block of the parent class is executed, then the parent class is constructed, and the parent class's construction method is executed to see if the subclass has no non-static code block, and if there is a non-static block of code that executes the subclass. The non-static code block of the subclass executes after the execution of the construction method. In summary, the static code block content takes precedence, then the non-static code block and the constructor method of the parent class, and then the non-static code block and the constructor method of the subclass.
Hands-on brain (class and object jobs are submitted again)