Static block format:
static
{
}
Static blocks are executed when the class is loaded, and because when loading a subclass, the parent class will be preloaded, so if there is a static block in the parent class, it will be executed first. The subclass's static block is then executed immediately.
If there are multiple static blocks in a class, they will be executed in the order of coding.
Note: Since the class will only be loaded once, each static block will only be executed once.
Free block format:
{
}
Free blocks are executed when the class is instantiated.
If you want to instantiate a subclass, its constructor will first instantiate its parent class. Therefore, the free block of the parent class will be executed first, and then the construction of the parent class will begin. After the constructor of the parent class is executed, the operation right is returned to the constructor of the subclass, and the free block of the subclass begins to be executed.
Then start constructing the subclass.
The following is a combination of parent class static block, subclass static block, parent class free block, subclass free block, parent class constructor, Subclass constructor, an example of this keyword and super keyword, I feel it is worth analyzing and recording.
[java] view
plaincopy
package com.lsky.test;
- ##public class Father {
## private int sum; //Static block in parent class-
## static {
## System.out.println("Happy New Year 2010");
}
public void first(int i){
sum= i sum -1 i--;
System.out.println( sum);
}
//Parent class parameterless constructor
public Father (){
System.out.println("Good day");
}
-
//Constructor with parameters
- ## public Father(String s){
- System.out.println("We are students ");
- }
- //Free block in the parent class
- {
- System.out.println("Bodhi has no tree, and the mirror is not a stand. There is nothing in the first place, so how can it cause dust");
- }
- }
- ##package com.lsky.test;
# public class Son extends Father { -
private int sum;
-
private static Father father;
-
//Parameter-containing constructor (parameter-containing constructor) in the subclass
-
public Son(Father father){
-
this();
-
father.first(8);
-
}
-
//No-parameter constructor (no-parameter constructor) in the subclass
-
public Son(){
-
# this.punk(6);
}
- ## public void first(int i){
## sum=sum i ; System.out.println(sum); } //Static block in subclass static{ -
## Father f=new Son();
## f.first(4);
}
- ## int punk(int sum){
- sum=sum;
- return sum;
- }
- //Free block in subclass
- {
- System.out.println("There was a time when the sea was difficult to overcome, except for Wushan, it was not a cloud");
- }
- public static void main(String argsp[]){
Son son=new Son(new Son());
- ## }
- }
[java] view
plaincopy
##//The output result is:
-
## Happy New Year 2010
-
There is no Bodhi tree, and the mirror is not a stand. There is nothing in the first place, so where can we get the dust?
-
A beautiful day
-
Once upon a time, the sea was difficult to overcome, except for Wushan, it was not a cloud
-
4
-
Bodhi has no tree, and the mirror is not a stand , there is nothing in the first place, where is the dust
-
A beautiful day
-
Once the sea was filled with water, except for Wushan, it is not a cloud
-
There is no Bodhi tree, and the mirror is not a stand. There is nothing in the first place, so where can we cause dust?
-
Good day
-
It used to be difficult to overcome the sea, except for Wushan, it is not a cloud
-
8
The above is the detailed content of What is the execution order of Java static blocks, free blocks and constructors. For more information, please follow other related articles on the PHP Chinese website!