Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What's the difference between Class.forName and classloader add-in classes?

2025-04-13 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

Today, I will talk to you about the difference between Class.forName and classloader add-on classes, which may not be well understood by many people. In order to make you understand better, the editor has summarized the following contents for you. I hope you can get something according to this article.

I. Class loading mechanism

If you look at it in detail, it can be divided into five stages:

(1) load: the java class runtime will generate a class bytecode file, the loading process is to go to our operating system to find the class file.

(2) Link: this process is to load the class file into the java virtual machine.

(3) initialization: initialize according to the class file in the virtual machine.

(4) use: this process is well understood.

(5) Uninstall: after using, the java virtual machine will be cleaned.

For class.forName and classloader, this is the first process, the loading process. However, although there are some similarities between the two, they are quite different.

Second, analyze Class.forName () and ClassLoader.loadClass

Let's use the code and see how to use it first. Pay attention to the scope of the package to avoid being unable to load.

Step 1: define the User class

Step 2: test

We used two loading methods in the test method above. Now let's test it:

It doesn't feel any different. Now is to give a general use, let's analyze the difference between them.

II. Differences

1 、 class.forName

The former class.forName () not only loads the class's .class file into jvm, but also interprets the class and executes the static block in the class. Note that the static block here refers to some data at the time of class initialization. But classloader does not, want to understand this reason, or directly to the source code to have a look.

In this source code, we will find that what is really implemented at the bottom is the forName0 method, so what do these parameters mean?

(1) className: indicates the name of the class we want to load

(2) true: whether the Class must be initialized after it is loaded. If you don't initialize, you don't execute static code, that is, static code, which defaults to true, which is the default implementation of class initialization.

(3) ClassLoader.getClassLoader (caller): represents the classloader, where you will find that forNanme is actually loaded using the ClassLoader classloader.

(4) caller: specifies the class loader.

The Class.forName (className) method, which is actually called internally, is Class.forName (className,true,classloader); the second boolean parameter indicates whether the class needs to be initialized, and Class.forName (className) requires initialization by default. Once initialized, the static block code execution of the target object is triggered, and the static parameter is initialized again.

2 、 classloader

In the above case, we found that classloader did not initialize the static block, and it is best to look at the source code.

First, let's go to the source code in the loadclass method.

Public Class loadClass (String name) throws ClassNotFoundException {return loadClass (name, false);}

This step does not seem to understand, it does not matter what is really implemented here is the internal loadclass, let's go in and have a look.

This is the real implementation method, the steps here are actually very simple, the general process is to determine whether the class has been loaded, if it is loaded, then reload, if not, then use the parent delegation principle to load. No initialization is specified at load time.

So now there is basically little difference between them, so let's sum it up:

(1) class.forName (), in addition to loading the .class file of the class into jvm, interprets the class and executes the static block in the class. Of course, you can also specify whether static blocks are executed.

(2) classLoader only does one thing, that is, it loads .class files into jvm, does not execute the contents of static, and executes static blocks only in newInstance.

Why do database links use Class.forName (className)

Static {try {java.sql.DriverManager.registerDriver (new Driver ());} catch (SQLException E) {throw new RuntimeException ("Can't register driver!");}} after reading the above, do you have any further understanding of the difference between Class.forName and classloader add-in classes? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report