Java class loading

Source: Internet
Author: User
Classes in Java are dynamically loaded. Let's take a look at our common class loading methods first. We have a perceptual knowledge before we can further
For further discussion, class loading is nothing more than the following three methods.
Class {}
Class B {}
Class C {}
Public Class Loader {
Public static void main (string [] ARGs) throws exception {
Class AA = A. Class;
Class BB = Class. forname ("B ");
Class cc = classloader. getsystemclassloader (). loadclass ("C ");
}
}
Let's first look at the. Class literal method. Many people may not know this method, because this method is not a general Java syntax.
Through javap, we can find that this method is roughly equivalent to defining a static member variable.
Static class $0; (the numbers below are increasing)
You can try to define another static class $0, which should receive a compilation error (repeated definition ). Class AA = A. Class;
It is equivalent
If (class $0 = NULL ){
Try {
Class. forname ("");
}
Cacth (classnotfoundexception e ){
Throw new noclassdeffounderror (E );
}
}
Class AA = Class $0; it is clear that the literal definition of this type is not the method of loading the class, but is processed by the compiler.
The class. forname method is used, but this method has a major advantage that you do not need to handle exceptions, because
If the class cannot be found during compiler processing, a noclassdeffounderror will be thrown. Maybe you think you need to handle it
Classnotfoundexception. In fact, in 99% cases, we can consider this exception as an error.
This method is more concise in most cases. The most common method is the class. forname method, which is also a common upper-layer call. This method has two reloads,
Many may ignore the second method.
Public static class forname (string name) throws classnotfoundexception
Public static class forname (string name, Boolean initialize, classloader loader) throws classnotfoundexception the second method has two more parameters, and the second parameter indicates whether to initialize, the third parameter is the specified class loader.
In the above example:
Class BB = Class. forname ("B"); equivalent
Class BB = Class. forname ("B", true, loader. Class. getclassloader ());
The initialization parameter of this class should be detailed here. If this parameter is false,
The static member in the class will not be initialized, and the static statement block will not be executed.
That is, although the class is loaded but not initialized, it will still be initialized during the first use.
So sometimes we can see statements such as class. forname ("XXX"). newinstance (). Why do we need to create
What about unused instances? But to ensure that the class is initialized (compatible with the previous system ).
In fact, the second method is relatively difficult to use. You need to specify the Class Loader. If you do not specify it and have not installed the security manager,
Classes cannot be loaded. You only need to take a look at the specific implementation to understand. The most essential method is to directly use classloader to load, and all classes are finally loaded through classloader,
Class cc = classloader. getsystemclassloader (). loadclass ("C ");
The system class loader is used to load a class in a direct way, but it is a pity that the class is loaded in this way,
Class is not initialized (that is, Initialization is delayed to be used). However, we can also learn from the above experience to load
Then instantiate an object class cc = classloader. getsystemclassloader (). loadclass ("C"). newinstance ().
The system class loader is used here, which is also the most common class loader. Find the class to be loaded from classpath.
By default, Java has three types of loaders: bootstrap loader, extended class loader, and system class loader.
Class loading in Java has a standard hierarchy. To understand the process of class loading, you need to know which class is named
Loading, which classes are loaded by a Class Loader, and so on, requires a deep understanding of the nature of classloader.
The above is just a surface of class loading. We will also discuss deep-seated things.

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.