JAVA namespaces for different class loaders

Source: Internet
Author: User

JAVA namespaces for different class loaders

I used to have the following questions:

In JAVA, classes loaded by different class loaders are located in different namespaces in virtual machines, and classes in different namespaces are invisible to each other.

This leaves me confused: If A Class A uses the java. util. List class, why is there no error at runtime. Because according to the parent-parent delegation mechanism of class loading, Class A written by myself is generally loaded by the system class loader, while java. util. the List must be loaded by the start class loader (also called the Root class loader). Therefore, these two classes should not be in the same namespace. Why can Class A still access java. util. List during runtime?

The reason is as follows:

First, we understand that each JAVA class has a corresponding type in the virtual machine after being loaded.

Another concept is as follows: if Class A is loaded by the system class loader, the system class loader is the corresponding type of Class A in the virtual machine.Initial Class Loader

The Java Virtual Machine maintains a table for each class loader, which records all types of the class loader as the initial class loader. When loading a class, the virtual machine uses these lists to determine whether a class has been loaded by a specific class loader (If this type is in the list of the current class loaders, it indicates that it has been loaded and will not be loaded ).

Return to the example where A uses java. util. List. When A is loaded and parsed to A and uses List, it requests to load java. util. List. Based on the loading principle of the class and the parent-parent delegation mechanism. Will first ask the class loader of Class A, that is, the system class loader loads java. util. list, of course, the system class cannot load this List, so it will delegate to its parent loader, that is, the extended Class Loader. Similarly, it will eventually load this java according to the class loader. util. list, and return successfully.

According to the Java Virtual Machine specification, all class loaders involved in this process-from the system class loader to the root class loader, are involved in the loading, are marked as the initial class loaders of this type. In other words, the Java Virtual Machine adds a type to the table maintained by the First Class Loader to indicate that the loader is the initial class loader of this type.

This makes it difficult to understand why Class A can use java. util. List. Although they are not loaded by A loader, they maintain type A and type List in the table of the system class loader.


Go deep into the classloader in the Java Virtual Machine to explain why the mode is parent-parent delegation instead of single-parent delegation? Mentioned in the Class Loader

In-depth explanation of the Class Loader in the Java Virtual Machine, why is the parent-parent delegation mode not the single-parent delegation mode?
This is the name issue. invoke commands are used in essence, whether inherited from a class or implemented multiple interfaces,
A single parent is just a name.
Class information stored in the method area. Namespace refers to the namespace of all classes. Unlike the namespace of class instances, developers cannot forcibly specify the namespace in which class instances are placed. You do not need to worry about the release of memory resources, because it is transparent. The only reason is that the method area not only stores the type information, but also provides many methods for indexing or other necessary operations. These methods are almost invisible to developers. The method area is more like a container that provides management and other operations.

What is the difference between java packages and namespaces?

1. java only has classes, so you only need to define the classes in their respective packages to avoid repetition. c ++ namespace can include not only classes, but also functions, variables, templates, and so on.
2. Use the packge package name in the first line of the class in java to put the class in the package.
In c ++, use namespace name {class, function, variable, template, etc };

In general, C ++ has more header files, which can be used for Function Definition and variable declaration ..

The key to this problem lies in understanding the characteristics of the two languages.
Java Contains classes in the package, and the names cannot be repeated.
C ++ can have the same name, but the suffix cannot be the same ..

Is a type of classification packaging for source programs, easy to call
Reference: zhidao.baidu.com/question/40551357.html

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.