Java newinstance () and new comparison

Source: Internet
Author: User

-------------------------------------------------------1----------------------------------------------

• First functionally: They are all instances of creating a class new is a keyword
Newinstance (), is a method
• Where is the difference between them?
1, newinstance (), using the class loading mechanism to create a new instance
2, New directly create an instance
So why are there two ways to create an instance?
The main consideration is the scalable, scalable, reusable design idea of software.
Note: When using the Newinstance () method, the object must have a default parameterless construction method;


Use class loading mechanism: can be very flexible, create instances of classes, and later replace the class, no need to modify the source code.
Some people would say that when calling the Newinstance method, not to return an instance object, it is not to be modified. The return is always a parent class, or an interface that does not return a subclass at all, so there is no need to worry



-----------------------------------------------------2--------------------------------------------------------- ---------
Used on different occasions. If you know exactly what you want, direct new.
If it is loaded from the configuration, use the newinstance. The advantage of using newinstance is that you do not need to change the code, you can change the implementation.


----------------------------------------------------3---------------------------------------------------------- ---------

When initializing a class and generating an instance, the Newinstance () method and the new keyword have the most important difference except that one is a method and one is a keyword.
The difference is that the object is created in a different way, using the class-loading mechanism, which creates a new class. So why are there two ways of creating objects? This mainly considers software design ideas such as scalable, scalable and reusable software.



Factory patterns in Java often use the newinstance () method to create objects, so you can find specific answers from why you use Factory mode. For example:


Class C = Class.forName ("Example");


Factory = (exampleinterface) c.newinstance ();





Where Exampleinterface is the example interface, it can be written in the following form:


String className = "Example";


Class C = Class.forName (ClassName);


Factory = (exampleinterface) c.newinstance ();





Further can be written in the following form:


String className = readfromxmlconfig;//get strings from XML configuration file


Class C = Class.forName (ClassName);


Factory = (exampleinterface) c.newinstance ();





The above code already does not exist example class name, its advantage is, regardless of example class how change, the above code is invariable, can even replace example brothers class Example2, Example3, Example4 ..., As long as they inherit exampleinterface.





From the JVM's point of view, when we use the keyword new to create a class, this class can be not loaded. However, when using the Newinstance () method, you must ensure that: 1, this class has been loaded, 2, this class has been connected. The static method forname (), which completes the two steps above, is done by calling the boot class loader, the loader that loads the Java API.





As you can see now, newinstance () actually breaks down the new method into two steps, which is to first call the class load method 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, and provide a means of decoupling.





Finally, use the simplest description to differentiate between the new keyword and the Newinstance () method:


Newinstance: Weak type. Low efficiency. Only parameterless constructs can be invoked.


NEW: Strongly typed. relatively efficient. Can invoke any public construct.

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.