Static blocks, building blocks, construct the object constructor call the order

Initialization block

Performing static blocks, building blocks before calling the object is constructed, we need to know initialization block.
In a class declaration, a plurality of code blocks may contain, as long as the object class structure, these blocks will be executed.
E.g:

class Employee{
	private static int nextId;
	private int id;
	private String name;
	private double salary;

	{
		id = nextId;
		nextId++; 
    }
	...
}

No matter which configuration is configured in the object using the above code, id fields are initialized during object initialization block. First run initialization block, before operating configuration body portion.

Static initialization block

But let the class constructor behavior depends on the order of data fields declarations, can easily lead to errors. It can be used to initialize the static field by using a static initialization block.
The code in one block, and marked keyword static:

static{
	Random generator = new Random();
	nextId = generator.nextId(10000);
}

Call construct objects order

Enter the following code in a compiler

public class Test5 {
	
	public Test5() {
		System.out.println("构造函数");
	}
	static {
		System.out.println("静态块");
	}
	{
		System.out.println("构造块");
	}
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Test5 t = new Test5();
	}

}

Here are the results of program execution:
Here Insert Picture Description
We found that the first execution of the block is static and is the last building block is the constructor.
We give another example to test whether this order:

public class EmployeeTest
{
   public static void main(String[] args)
   {
      Employee tom = new Employee("tom",20);
      System.out.println(tom);
      System.out.println(tom.getId());
      Employee jerry = new Employee("jerry",19);
      System.out.println(jerry);
      System.out.println(jerry.getId());
   }
}

class Employee
{
   
   private String name = "lisi";
   {
	   System.out.println("初始化1");
	   name = "wangwu";
	   age = 20;
   }
   private int age = 30;
   static{
	   System.out.println("静态初始化1");
	   id = 40;
	   System.out.println("id1:" + EmployeeTest.id);
	   System.out.println("liuliu开始");
	   Employee employee = new Employee("liuliu",50);
	   System.out.println(employee);
	   System.out.println("liuliu id:" + employee.getId());
	   System.out.println("liuliu结束");
   }
   private static int id = 30;
   {
	   System.out.println("初始化2");
	   name = "zhaosi";
	   age = 40;
   }
   static {
	   System.out.println("静态初始化2");
	   id = 80;
   }

   public static int getId() {
	   return id;
   }

   public String getName()
   {
      return name;
   }
   public void setName(String name) {
	   this.name = name;
   }
   public Employee(String name,int age) {
	   super();
	   System.out.println("有参构造器");
	   this.name = name;
	   this.age = age;
   }
   public Employee() {
	   super();
	    System.out.println("无参构造器");
   }

@Override
public String toString() {
	return "Employee [name=" + name + ", age=" + age + "]";
}
}

Implementation of the results:

Here Insert Picture Description
According to the results found in our validation is correct.
So for the case of a class, static blocks, building blocks and the sequence constructor object configured to call:
1. Static blocks
2. building block
3. Constructor

It is not a class for parent and child classes on their static blocks, building blocks, construct the object constructor call order is what look like?
We can write a simple example:

class HelloA{
	public HelloA() {
		System.out.println("HelloA");
	}
	{
		System.out.println("I am class A");
	}
	static {
		System.out.println("static A");
	}
}

public class HelloB extends HelloA{
	public HelloB(){
		System.out.println("HelloB");
	}
	{
		System.out.println("I am class B");
	}
	static {
		System.out.println("static B");
	}
	public static void main(String[] args) {
		new HelloB();
	}
}

The following are the results:
Here Insert Picture Description
in the same class, we need to know when to load the entire program to call the constructor, the static load block execution priority.
When to call the subclass priority call the parent, the parent class will be loaded first, the first two of static A static B
priority call when calling the parent class constructor subclass constructor, the constructor call, initialization block will first are performed after the constructor, thus giving rise to I class A HelloA am
finally subclass constructor is executed, performs initialization block before calling the constructor, the final two as I am class A, I am class B

Guess you like

Origin blog.csdn.net/fight252/article/details/89234679