Home > Java > javaTutorial > body text

What's the difference between 'Class.forName()' and 'Class.forName().newInstance()' in Java?

DDD
Release: 2024-11-08 17:34:02
Original
547 people have browsed it

What's the difference between

Understanding the Difference between "Class.forName()" and "Class.forName().newInstance()": A Comprehensive Explanation

The Java language provides several methods for instantiating classes. Two commonly used methods are "Class.forName()" and "Class.forName().newInstance()". While they share similarities, there is a significant distinction between their functionality.

Class.forName()

The "Class.forName()" method takes a fully qualified class name as an argument and returns a "Class" object representing that class. This object provides metadata about the class, including information about its fields, methods, and constructors. However, it does not actually create an instance of the class.

Class.forName().newInstance()

In contrast, "Class.forName().newInstance()" takes the "Class" object returned by "Class.forName()" and attempts to create a new instance of the corresponding class using the default constructor. If successful, it returns a reference to the newly created object. Here lies the primary difference between the two methods: "Class.forName()" only returns a "Class" object, while "Class.forName().newInstance()" creates an actual instance.

Example:

Consider the following Java code:

package com.example;

public class DemoClass {

    public DemoClass() {
        System.out.println("DemoClass instantiated");
    }

    public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException {
        // Get the Class object
        Class clazz = Class.forName("com.example.DemoClass");

        // Create an instance
        DemoClass instance = (DemoClass) clazz.newInstance();
    }
}
Copy after login

When this code is executed, "Class.forName("com.example.DemoClass")" returns the "Class" object associated with the "DemoClass" class. Then, "clazz.newInstance()" creates a new instance of the "DemoClass" class, calling its default constructor, which prints "DemoClass instantiated."

Dynamic Class Loading:

One key advantage of "Class.forName()" is its ability to dynamically load classes at runtime. Suppose your application needs to instantiate a class based on user input or configuration. In that case, you can use "Class.forName()" to dynamically load that class and create an instance without hard-coding the class name.

Conclusion:

In summary, the difference between "Class.forName()" and "Class.forName().newInstance()" lies in their purpose. "Class.forName()" returns a "Class" object that provides metadata about the class. In contrast, "Class.forName().newInstance()" creates a new instance of the class using the default constructor, similar to the traditional "new" operator.

The above is the detailed content of What's the difference between 'Class.forName()' and 'Class.forName().newInstance()' in Java?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template