What is the difference between the new keyword and the newinstance () method in Java?

Source: Internet
Author: User

1.new can call a constructor with parameters, newinstance not.

2.new is a Java keyword used to construct an instance of a class. Newinstance is a method of Class, but the same is true of the two notation.

3.newInstance: Weak type. Low efficiency. Only parameterless constructs can be called. NEW: Strongly typed. relatively efficient. Can invoke any public construct.

4.new is the keyword, used to produce objects, can call the class to expose any construction method and newinstance is reflected when used, can only call the parameterless construction method, if you want to call the constructor method with parameters, you can use the constructor newinstance (Object... initargs) method.

Note: In the open source Chinese community to see a more vivid metaphor:

New is to cook, cook and eat for yourself. Newinstance is eating outside to make you eat.

Self-made can guarantee safety, and can have multiple flavors, what sauce, put how much to set. The new object does not have an exception because the class cannot be found, and you can call more than one constructor and go to the new object in your own taste to do something that suits your tastes.

Eat outside, you may order the dishes that are not in the shop, or the taste is not right, you want to eat tomato sauce, but others only peanut butter. And you don't know if the dishes are clean. Newinstance is the same, it is possible that you want the object he does not exist, or the parameters are not correct.

What is the difference between the new keyword and the newinstance () method in Java?

Related Article

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.