Java class loading sequence, object creation

We know that before the object is created, we will first load the class, if the class contains static elements (attributes and methods), will be loaded static elements before you begin to create the object. Nothing more than create the object constructor is called, before creating the object, if you define a block, the block will be executed first, if there is static modified static block, the block will first perform static than ordinary blocks.
Before there inheritance of object creation process, the problem becomes a little complicated, create a subclass object object subclass constructor call, we will first call the constructor of the parent class. So Father analogy subclasses loaded earlier.
java class loading mechanism is actually a very complex issue, I have here the object of the process of creating in some places is still not right, but in understanding terms, this order is basically talking about the pass.

public class Animal {
	public String test = "AnimalField";
	public static String testStatic = "AnimalStaticField";
	public Animal(){
		System.out.println("我是animal中默认无参数的构造方法");
	}
	{
		this.testAnimal();
		System.out.println("我是animal中的普通程序块"+test);
	}
	static {
		testStatic();
		System.out.println("我是animal类中静态程序块"+testStatic);
	}
	public void testAnimal(){
		System.out.println("我是animal类中的普通方法");
	}
	public static void testStatic() {
		System.out.println("我是animal类中的静态方法");
	}
}
public class Person extends Animal{
	public String test = "personField";
	public static String testStatic = "personStaticField";
	public Person(){
		System.out.println("我是person中默认无参数的构造方法");
	}
	{
		this.testPerson();
		System.out.println("我是person中的普通程序块"+test);
	}
	static {
		testStatic();
		System.out.println("我是person类中静态程序块"+testStatic);
	}
	public void testPerson(){
		System.out.println("我是person类中的普通方法");
	}
	public static void testStatic() {
		System.out.println("我是person类中的静态方法");
	}
}
public class Test {
	public static void main(String[] args) {
		Person person = new Person();
	}
}

Output:
Here Insert Picture Description
Here Insert Picture Description
In conclusion java class order and the creation of an object is loaded :( first four steps is the class loading order)

  1. Load class template parent class
  2. Parent will have its own static spatial (static properties, static methods, static blocks, to perform static block)
  3. Load class template subclass
  4. Subclasses produce their own static spatial (static properties, static methods, static blocks, to perform static block)
  5. Open space objects
  6. Loading the parent class of non-static member (properties, methods, blocks, constructor)
  7. Block run a parent, the parent class constructor method performed
  8. Non-static member loaded subclass (attributes, methods, blocks, constructor)
  9. Block performs subclass, the subclass constructor method performed

Description:

  • Class template stored information only class, will not be executed, but when generating a static space, static block is executed (the default block is executed).
  • In fact, the underlying load to load, the execution will be uniformly implemented.

java学习的小白一个,若写的有什么不对的地方,欢迎批评否证。

Published 46 original articles · won praise 16 · views 2629

Guess you like

Origin blog.csdn.net/qq_43598138/article/details/104179137