Home > Java > javaTutorial > Java class loaders and namespaces

Java class loaders and namespaces

黄舟
Release: 2016-12-20 13:32:59
Original
1712 people have browsed it

Abstract
                                                                                                                                                                                       Class loaders, and related parent delegation models, namespaces, runtime packages and other concepts, and also discuss some in-learning Issues that are easily confused.


Function and classification of class loader

As the name suggests, class loader is used to load classes into the JVM. The JVM specification defines two types of class loaders: bootstrap and user-defined class loader.

Bootstrap is the class loader that comes with the JVM, which is used to load core class libraries, such as java.lang.*, etc. For example, java.lang.Object is loaded by bootstrap.

Java provides the abstract class ClassLoader, and all user-defined class loaders are instantiated from subclasses of ClassLoader. System Class Loader is a special user-defined class loader, which is provided by the JVM implementer. It loads user classes by default when the programmer does not specify a loader. The system class loader can be obtained through the ClassLoader.getSystemClassLoader() method.



Example 1, test the ClassLoader of the JVM you are using

/*LoaderSample1.java*/

public class LoaderSample1 {
 public static void main(String[] args) {
 Class c;
 ClassLoader cl; cl = ClassLoader.getSystemClassLoader();
System.out.PRintln(cl); while (cl != null) {
cl = cl.getParent();
System.out.println(cl);
       }
                                                                                                                                                                                                                                                                                c = Class.forName("java.lang.Object");
cl = c.getClassLoader(); System.out.println("java.lang.Object's loader is " + cl); cl = Class.forName ("LoaderSample1");
         cl = c.getClassLoader(); 
          System.out.println("LoaderSample1's loader is " + cl);
                                                                                                                                                                                                     ("Loader "LoaderSample1; e.printStackTrace();
}
}
}

The above is the content of Java class loader and namespace. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template