Suppose that there is a class,classloader first load it into the memory of the code segment (memory stored in the snippet), standing in the angle of ClassLoader, the memory of a class is one of the objects, This object is Xx.class, which is actually the object of class. After load class, find the main function to start execution, and then put a lot of other classes load in, dynamic loading mechanism.
To test the dynamic loading mechanism:
New Project Reflection,new a class,testdynamicloading:
Public classtestdynamicloading { Public Static voidMain (string[] args) {NewA (); System.out.println ("*****-------------------------******"); NewB (); NewC (); NewC (); NewD (); NewD (); }}classa{}classb{}classc{//Static statement block, called only once. Static{System.out.println ("CCCCCCCCCCCCCCCCCCCCCCCCCCCC"); } }classd{//A dynamic statement block that is called once every new object is created. Not much to use. {System.out.println ("DDDDDDDDDDDDDDDD"); }}
Right-click Run as-----> Run configurations.
Program arguments: Command line parameters
VM arguments: The parameters of the virtual machine, the Java program runs when the Java command line gives you the specified virtual machine parameters
-verbose:class: Verbose output.
---> Run
You can see the output of the console, and the steps to perform the program are:
1, first load class A,
2, System.out.println ("* * * * * * * *-------------------------******");
3, Load Class B,
4, Load Class C, and the static statement block executes only once.
5, Load Class D, dynamic statement block executed 2 times.
The dynamic loading mechanism of Java is the same and is verified.
[Loaded A from file:/d:/eclipse/gongzuokongjian/reflection/bin/]* * * * *-------------------------****** [Loaded B from File:/d:/eclipse/gongzuokongjian/reflection/bin/][loaded C from file:/d:/eclipse/ gongzuokongjian/reflection/bin/]cccccccccccccccccccccccccccc[loaded D from file:/d:/eclipse/ gongzuokongjian/reflection/bin/]dddddddddddddddddddddddddddddddd
Java Dynamic loading mechanism