What's the difference between newinstance () and new

Source: Internet
Author: User
What is the difference between newinstance () and new.

Welcome to add my QQ group: 1543150073

I used to load objects with Newinstanc () when I was doing MBB, and when I initialized a class to generate an instance, newinstance () and new were different.
The difference between using newinstance and new is that the object is created differently, using the class loading mechanism, and why there are two ways of creating objects. This is going to be explained in terms of scalable, scalable, reusable software.
Factory patterns in Java often use Newinstance to create objects, so you can find specific answers from why you use Factory mode.
For example:
Class C = class.forname ("A"); factory = (Ainterface) c.newinstance ();
Where Ainterface is the interface of a, if you write this, you may understand:
String className = "A"; Class C = class.forname (className); factory = (Ainterface) c.newinstance ();
Further, if you write below, you may understand:
String className = readfromxmlconfig;//from XML configuration file Class c = class.forname (className); factory = (Ainterface) C.newinstance ();
The above code eliminates the Class A name, the advantage: No matter how a class changes, the above code is unchanged, can even replace a brother Class B, C, D .... Wait, as long as they inherit ainterface can.
From the JVM's point of view, when we use new, the class to be new can be loaded without loading;
However, when using newinstance, you must ensure that: 1, this class has been loaded, 2, this class has been connected. The static method Forname () method of class, which completes the above two steps, calls the Startup class loader (the one that loads the Java API).
With the understanding on the JVM above, we can say that newinstance actually breaks the new way into two steps, that is, to call the class's Load method first to load a class and then instantiate it.
The benefits of this step-by-step are obvious. We can get more flexibility when calling the static Load method of class forname, which gives us the means to decouple.

[Add:]
Newinstance: Weak type. Low efficiency. Only parameterless constructs can be invoked.
NEW: Strongly typed. relatively efficient. Can invoke any public construct.
Newinstance () is an inevitable choice for IOC, reflection, interface programming and dependency inversion, and new can only realize the instantiation of concrete class, not suitable for interface programming.
Inside is an object constructed from the default constructor of this class, which throws instantiationexception if there is no default constructor, and throws illegalaccessexception if there is no permission to access the default constructor.

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.