Java class loading performance form

xiaoxiao2021-04-08  276

The classes in Java are dynamically loaded. Let's first look at the type of way we are using, first have a sense of understanding, in order to further

In-depth discussion, class loading is nothing more than three ways.

Class a {}

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 (). LoadingClass ("c");

}

}

Let's see the .class literal method, many people may not know this approach because this usage is not general Java syntax.

Through Java, we can find that this approach is substantially equivalent to define a static member variable.

Static Class Class $ 0; (number of numbers behind)

You can try again to define a Static Class Class $ 0, you should receive a compilation error (repeated definition).

Class aa = a.class;

Be equivalent to

IF (Class $ 0 == NULL) {

Try {

Class.Forname ("a");

}

CACTH (ClassNotFoundException E) {

Throw new Noclassdeffounderror (E);

}

}

Class aa = Class $ 0;

It can be clearly seen that the word definition of this class is not a way to load, but is processed by the compiler, substantive

The Class.Forname method is used, but there is a big advantage in this way that it is no need to deal with exception, because

If the compiler is processed, if you can't find the class, you will throw a NoclassDefoundError. Maybe you feel that you need to deal with

ClassNOTFoundException This exception is in fact, in fact, we can think of this exception is an error.

So most cases we use this way more concise.

The most common way is Class.Forname mode, which is also a universal upper layer call. This method has two overloads,

Many people may ignore the second method.

Public Static Class Forname (String Name) throws ClassNotFoundException

Public Static Class Forname (String Name, Boolean Initialize, ClassLoader Loader) THROWS CLASSNOTFOONDEXCEPTION

There are two parameters behind the second method, and the second parameter indicates whether it is initialized, 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 ());

Here you must talk more about this type of initialization, if this parameter is false,

Static members in the class will not be initialized, and the Static statement block will not be executed.

That is, although the class is loaded, it is not initialized, but it will still be initialized during the first use.

So we sometimes see class.Forname ("xxx"). NewInstance () This statement, why you want to create a

Doesn't need examples? But to ensure that classes are initialized (compatible with previous systems).

In fact, the second method is more difficult, need to specify the class loader, if not specified, no installation of the security manager, is unable to load, as long as you look at the specific implementation.

The most essential way is of course directly loaded with ClassLoader, and all classes are ultimately loaded through ClassLoader.

Class CC = ClassLoader.getsystemClassLoader (). LoadingClass ("c");

Here, by using the system class loader to load a certain class, it is very straightforward, but unfortunately, it is loaded in this way.

The class is not initialized (that is, when the initialization is delayed to really use). However, we can also learn from the above experience.

After instance, an object class cc = classloader.getsystemclassloader (). LoadingClass ("c"). NewInstance ().

Here, the system class loader is used, and the most commonly used class loader is used to find the class to be loaded from the ClassPath.

There are three types of loaders in Java: boot class loader, extended class loader, system class loader.

The classes in Java have a standard hierarchy. If we need to understand the process of the class load, you need to know which class is

Loading, a class loader loads which classes, etc., you need to understand the essence of ClassLoader in depth.

The above is just something that is loaded, we will also discuss deep things.

转载请注明原文地址:https://www.9cbs.com/read-132883.html

New Post(0)