Summary of knowledge points of Java class loading process

1. When java is executed , classes are dynamically loaded , not loaded at one time.

 

2. Layering of classloader :

The top layer is bootclassLoader , followed by extension (Extesion classloader) , application (Application classLoader , user-defined class) , and other loaders

Note: When classLoader loads a class , it first finds whether the loader of the previous layer has been loaded . If it has been loaded , it will not be loaded again .

Function: good safety! For example: String.class written by yourself will never be executed, only the String.class of the system will be executed

 

classloader example program:

public class TestClassLoader {

	/**
	 * Demonstrate knowledge points of classloader
	 * @author cdzhujun
	 */
	public static void main(String[] args) {
		//classloader classification
		//Note: BootStrap ClassLoader has no name
		System.out.println(String.class.getClassLoader());
		System.out.println(com.sun.crypto.provider.DESedeKeyFactory.class.getClassLoader().getClass().getName());
		System.out.println(TestClassLoader.class.getClassLoader().getClass().getName());
		
		
		System.out.println("——————");
		/*
		 * classLoader first finds out if the loader of the previous layer has been loaded when loading a class. If it has been loaded, it will not be loaded again.
		 * Function: good safety! For example: String.class written by yourself will never be executed, only the String.class of the system will be executed
		 */
		ClassLoader c = TestClassLoader.class.getClassLoader();
		//Print out the parent classloader
		while(c != null) {
			System.out.println(c);
			c = c.getParent();
		}
	}
}

 Results of the:

null
sun.misc.Launcher$ExtClassLoader
sun.misc.Launcher$AppClassLoader
——————
sun.misc.Launcher$AppClassLoader@7e820d53
sun.misc.Launcher$ExtClassLoader@c390508

 3. Static statement block: Execute once after loading.

Type such as:

static {
		System.out.println("This is a static block");
}

 4. Dynamic statement block: every new new object will be executed

Type such as:

{
		System.out.println("This is a dynamic statement block");
}

 5. The initialization sequence of the object: the content of the static code block is executed first, then the parent dynamic code block and construction method are executed, and then the subclass dynamic code block and construction method are executed.

Example program:

class ParentTest {
	public static String PARENT_STATIC_FIELD = "Parent class - static property";

	// parent class - static block
	static {
		System.out.println(PARENT_STATIC_FIELD);
		System.out.println("Parent class - static code block");
	}

	public static String parentField = "Parent class - non-static property";

	// parent class - non-static block
	{
		System.out.println(parentField);
		System.out.println("Parent class - non-static code block");
	}

	public ParentTest() {
		System.out.println("Parent class - no-argument constructor");
	}
}

public class StaticIniBlockOrderTest extends ParentTest {
	public static String STATIC_FIELD = "Static property";

	// static block
	static {
		System.out.println(STATIC_FIELD);
		System.out.println("Static code block");
	}

	public String field = "Non-static property";

	// non-static block
	{
		System.out.println(field);
		System.out.println("Non-static code block");
	}

	public StaticIniBlockOrderTest() {
		System.out.println("No parameter constructor");
	}

	public static void main(String[] args) {
		StaticIniBlockOrderTest test = new StaticIniBlockOrderTest();
	}
}

 Results of the:

Parent class - static properties
Parent class - static code block
static properties
static code block
Parent class - non-static property
Parent class - non-static code block
Parent class - no-argument constructor
non-static properties
non-static code block
no-argument constructor

 The execution result already clearly shows the execution order of the code.

 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326587683&siteId=291194637