Http://d-y-zh.javaeye.com/blog/607027
Non-static statement block, first seen.
Public class test
{
Public static void main (string [] AGRs)
{
Son son = new son ();
}
}
Class super
{
Public super ()
{
System. Out. Print ("A/T ");
}
{
System. Out. Print ("B/T ");
}
Static
{
System. Out. Print ("C/T ");
}
}
Class son extends super
{
Public son ()
{
System. Out. Print ("d/t ");
}
{
System. Out. Print ("E/t ");
}
Static
{
System. Out. Print ("F/T ");
}
}
Static objects belong to classes, and non-static objects belong to instances.
The static statement is executed first before it is non-static.
In the above example, we also need to consider the inheritance issue.
First, the parent class static --> C
Then the subclass static --> F
A non-static block can be considered as the first part after the parent class constructor is executed in the constructor. (The first line of the constructor has a default Super () call, which is to call the parent class constructor ).
Execute the parent class constructor at this time:
Non-static block of the parent class --> B
Parent class constructor block -->
Then the sub-class constructor:
Subclass non-static block --> E
Subclass constructor block --> d
Summary:
C f B A E d
It is worth noting that static things are executed only once, that is, when classes are loaded.
If the main function is changed:
Java code
Public static void main (string [] AGRs) {son = new son (); son son2 = new son ();}
The execution result is as follows:
C f B a e d B A e d