Understanding of different ClassLoader namespaces in JAVA

Source: Internet
Author: User

There had been such a doubt:

It is said that in Java, classes loaded by different ClassLoader are located under different namespaces in the virtual machine, and classes under different namespaces are not visible to each other.

This gives me a puzzle: If you have a class A that uses the Java.util.List class, why there is no error at run time. Since Class A written by Class A is normally loaded by the system ClassLoader, and Java.util.List is definitely loaded by the Boot class loader (also called the root ClassLoader), the two classes should not be under a namespace because of the parental delegation mechanism loaded by classes. Why does Class A still have access to java.util.List at run time?

Now it's clear, for the following reasons:

First we understand that each Java class has a corresponding type in the virtual machine after it has been loaded.

The following concepts: If Class A is loaded by the system ClassLoader, then the system ClassLoader is the initial ClassLoader for the corresponding type of this a in the virtual machine

The Java Virtual machine maintains a table for each class loader, which records all types that use the ClassLoader as the initial class loader. When loading a class, the virtual machine uses these lists to determine whether a class has already been loaded by a particular classloader (if the type is in the list of the current ClassLoader, it is already loaded and is no longer loaded).

Go back to just to a using java.util.List example, when a is loaded, resolves to a using List, it will request to load the java.util.List. Based on the loading principle of the class and the parent delegation mechanism. Class A's ClassLoader is first requested, That is, the System class loader loads the java.util.List, which of course does not load the List, so it will delegate to its own parent loader, the extension classloader, and eventually the java.util.List will be loaded by the ClassLoader and returned successfully.

According to the Java Virtual Machine specification, all class loaders involved in this process-that is, from the system ClassLoader to the root classloader-are tagged as the initial class loader of that type. In other words, the Java Virtual machine adds a type to the table maintained by the first ClassLoader to indicate that the loader is the initial class loader of that type.

This makes it easy to understand why Class A can use Java.util.List. Although they are not loaded by a loader, type A is maintained in the System ClassLoader table, which maintains the type list.

Understanding of different ClassLoader namespaces in JAVA

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.