Home > Java > javaTutorial > body text

What is the difference between new and newInstance in Java?

不言
Release: 2018-09-21 16:33:34
Original
2126 people have browsed it

This article brings you what is the difference between new and newInstance in Java? It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

When initializing a class and generating an instance; what is the difference between newInstance() and new?

There is a difference between using newInstance and using new. The difference lies in the way of creating objects. The former It uses the class loading mechanism, so why are there two ways to create objects? This needs to be explained from the perspective of software ideas such as scalability, extensibility, and reusability.
The factory mode in Java often uses newInstance to create objects, so you can also find specific answers from why you should use the factory mode.
For example:

Class c = Class.forName(“A”);
factory = (AInterface)c.newInstance();
Copy after login

where AInterface is the interface of A. If you write like this, you may understand:

String className = “A”;
Class c = Class.forName(className);
factory = (AInterface)c.newInstance();
Copy after login

Further, if you write below, you may understand You will understand:

String className = readfromXMlConfig;//从xml 配置文件中获得字符串
Class c = Class.forName(className);
factory = (AInterface)c.newInstance();
Copy after login

The above code eliminates the name of class A. Advantages: No matter how class A changes, the above code remains unchanged, and you can even replace A’s sibling classes B, C, D…, etc. , as long as they inherit Ainterface.
From the perspective of jvm, when we use new, the class to be new does not have to be loaded;
But when using newInstance, we must ensure that:

1. This class has been Loading;

2. This class has been connected. It is the static method forName() method of the class that completes the above two steps. This static method calls the startup class loader (that is, the loader that loads the java API).

With the above understanding of JVM, we can say that newInstance actually decomposes the new method into two steps, that is, first calls the class loading method to load a certain class, and then instantiates it.
The benefits of this step-by-step approach are obvious. We can gain better flexibility when calling the class's static loading method forName, which provides us with a means of decoupling.

[Supplement:]
newInstance: Weak type. ineffective. Only constructors without parameters can be called.
new: Strong typing. Relatively efficient. Can call any public constructor.
newInstance() is an inevitable choice for implementing technical methods such as IOC, reflection, interface-oriented programming and dependency inversion. new can only implement the instantiation of specific classes and is not suitable for interface programming.
An object is constructed through the default constructor of this class. If there is no default constructor, an InstantiationException will be thrown. If there is no permission to access the default constructor, an IllegalAccessException will be thrown.

1, Code usage:

It can be seen that new and newInstance have the same effect.

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

Related labels:
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