Java class loader (zz javaeye)

Source: Internet
Author: User
After Reading Wang Sen's Java deep adventure, he made a simple study note.
1. the Java class loader loads classes on demand only when a class is to be used (a class is instantiated using the new keyword, the Class Loader loads the class and initializes it.
Class main:

Java code
  1. Public class main {
  2. Public static void main (string [] ARGs ){
  3. A A = new ();
  4. A. Print ();
  5. B = new B ();
  6. B. Print ();
  7. }
  8. }


Class A: Java code

  1. Public Class {
  2. Public void print (){
  3. System. Out. println ("using class ");
  4. }
  5. }

Class B: Java code

  1. Public Class B {
  2. Public void print (){
  3. System. Out. println ("using class B ");
  4. }
  5. }

Run: Java-varbose: Class main
Execution result:
E:/dev> JAVA-verbose: Class main
[Opened C:/program files/Java/jre1.5.0 _ 11/lib/RT. jar](The Class Loader first loads Rt. jar to load the basic class)
.
.
[Loaded main from file:/E:/dev/](The Class Loader loads the corresponding class and initializes it)
[Loaded a from file:/E:/dev/]
Using Class
[Loaded B from file:/E:/dev/]
Using Class B
2. Make Java programs dynamic
To achieve dynamic performance in an explicit way, we need to handle the details of class loading by ourselves.

Two methods:
|
+ -- Implicit: use the new keyword to let the Class Loader load the required class as needed
|
+ -- Explicitly:
|
+ -- Loaded by the forname () method of Java. Lang. Class
|
+ -- Loaded by the loadclass () method of Java. Lang. classloader

(1) Use class. forname ()
The class. forname () method has two overload methods:
+-Public static class forname (string classname)
|
+-Public static class forname (string classname, Boolean initialize, classloader loader)
Parameter description:
Classname-fully qualified name of the required class
Initialize-whether class initialization is required (static code block initialization)
Loader-Class Loader used to load classes
The forname () method that calls only one parameter is equivalent to class. forname (classname, true, loader ).
The two methods are connected to the native method forname0 (). The definition is as follows:
Private Static native class forname0 (string name, Boolean initialize, classloader loader) throws classnotfoundexception;
There is only one parameter's forname () method, and the final call is:
Forname0 (classname, true, classloader. getcallerclassloader ());
The forname () of the three parameters is called at the end:
Forname0 (name, initialize, loader );
Therefore, whether new is used to instantiate a class, or the class. forname () method with only one parameter is used, the steps of loading class + running static code block are hidden inside. When using the class. forname () method with three parameters,If the second parameter is false, the Class Loader only loads the class instead of initializing the static code block. The static code block is initialized only when the class is instantiated.The static code block is initialized when the class is first instantiated.

(2) directly use the Class Loader
+-Get the class to which the object belongs: getclass () method
|
+-Get the class loader of this class: getclassloader () method Java code

  1. Public class main3 {
  2. Public static void main (string [] ARGs) throws exception {
  3. Main3 main3 = new main3 ();
  4. System. Out. println ("prepare to load class ");
  5. Classloader loader = main3.getclass (). getclassloader ();
  6. Class clazza = loader. loadclass (ARGs [0]);
  7. System. Out. println ("instantiation Class ");
  8. A O1 = (a) clazza. newinstance ();
  9. }
  10. }

Levels of Class 3 Loaders

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.