[The difference between]newinstance () and new

Source: Internet
Author: User

When initializing a class to generate an instance, what is the difference between newinstance () and new?
The difference between using newinstance and new is that the object is created differently, the former is using the class loading mechanism, then why are there two ways to create objects? This is explained in terms of scalability, extensibility, reusable software. Factory mode in Java often uses 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 the following, you may understand:
String className = "A";
Class C = class.forname (ClassName);
Factory = (ainterface) c.newinstance ();

Further, if written below, you may understand:
String className = readfromxmlconfig;//getting a string from an 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 the Class A changes, the above code is unchanged, can even replace A's brothers B, C, D ... etc., as long as they inherit the ainterface can.
From the JVM's point of view, when we use new, the new class can not be loaded, but when using newinstance, it must be guaranteed: 1, this class has been loaded, 2, this class has been connected. The forname () method, which completes the two steps above, is the static method of class, which invokes the startup ClassLoader (that is, the loader that loads JAVAAPI).
With the understanding on the JVM above, we can say that newinstance actually breaks the new approach into two steps, that is, first calling class's Load method to load a class and then instantiating it. The benefits of this step are obvious. We can get better flexibility when invoking the static Load method of class forname, providing us with the means to decouple.

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

[The difference between]newinstance () and new

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.