ClassLoader. loadClass and Class. forName are different. loadclassforname

Source: Internet
Author: User

ClassLoader. loadClass and Class. forName are different. loadclassforname

Why the ClassLoader. loadClass (String name) and Class. forName (String name) is compared, because they can all know all the attributes and methods of any class at runtime. For any object, can call any of its methods and attributes.

Before comparing them, you must first understand the java class loading process.

The java class loading process is divided into three steps:

 

1: Load

Jvm loads the class file bytecode into the memory, and loads the static data into the type data in the Method Area of the runtime data zone, generate a class in the runtime data zone heap.

Java. lang. Class object, as the entry to access Class data in the method area.

*Explanation:The method area not only stores methods, but also class type information.

2. Link: perform the following verification, preparation, and resolution steps. The resolution steps are optional.

A: Verify: Check the correctness and security of the loaded class file.

B: preparation: allocate storage space for class variables and set the initial value of class variables. class variables are stored in the method area with the type information, and have a long life cycle. improper use may cause memory leakage.

*Release: The class variable isStaticVariable; the initial value refers to the default value of the class variable type, rather than the actual value to be assigned.

C: parsing: jvm converts the symbol reference in the constant pool to direct reference.

3: initialization: Execution class variable assignment and static code block

 

After learning about the class loading process, we will continue to compare the differences between the two:

  • Classloder. loaderClass (String name)

Actually, this method calls Classloder. loadClass (name, false) internally)

Method: Classloder. loadClass (String name, boolean resolve)

1: parameter name indicates the fully qualified class name of the class.

2: The resolve parameter indicates whether to parse. If the resolve parameter is true, this class is parsed.

  • Class. forName (String name)

Actually, this method calls Class. forName (className, true, ClassLoader. getClassLoader (caller) internally ))

Method: Class. forName0 (String name, boolean initialize, ClassLoader loader)

Parameter name indicates the fully qualified class name

The initialize parameter indicates whether to initialize the class. true indicates whether to initialize the class.

Classloader corresponding to the loader Parameter

  • The biggest difference between the two

The Class obtained by class. forName has been initialized.

The class obtained by Classloder. loaderClass is not linked yet.

  • How to Use

In some cases, you only need to know the existence of this Class without initialization, and use Classloder. loaderClass. In some cases, you must select Class. forName when initializing.

For example, the database Driver is loaded using Class. froName ("com. mysql. jdbc. Driver "),

Let's take a look at the Driver source code:

public class Driver extends NonRegisteringDriver implements java.sql.Driver {    public Driver() throws SQLException {    } 
static { try { DriverManager.registerDriver(new Driver()); } catch (SQLException var1) { throw new RuntimeException("Can\'t register driver!"); } }}

From the Driver source code, we can see that the Driver Class has only one static block, so that we can get DriverManager after initialization, So we choose to use Class. forName ()

 

Related Article

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.