Several ways to create objects in Java

Source: Internet
Author: User
Tags reflection

Transferred from: https://www.cnblogs.com/baizhanshi/p/5896092.html

As Java developers, we create many objects every day, but we often use dependency injection to manage systems such as spring to create objects, but there are many ways to create objects: Using the new keyword, using the class Newinstance method, Use the Newinstance method of the constructor class, use the Clone method, and use deserialization.

  1. Use the New keyword: This is our most common and simplest way to create objects, and in this way we can call any of these functions (without arguments and arguments). For example: Student Student = new Student ();
  2. Using the class Newinstance method: We can also use the class class's Newinstance method to create the object, which calls the parameterless constructor to create the object, such as: Student Student2 = (Student) Class.forName ("Root path. Student "). newinstance (); Or: Student stu = Student.class.newInstance ();
  3. Use the Newinstance method of the constructor class: This method is similar to the Newinstance method of class, and there is a newinstance method in the Java.lang.relect.Constructor class to create the object. We can call the parameterized and private constructors through this newinstance method. such as: constructor<student> Constructor = Student.class.getInstance (); Student stu = Constructor.newinstance (); The two newinstance methods are what we call reflection, in fact the Newinstance method of class calls constructor Newinstance method internally. This is why many frameworks use the latter, such as spring, Hibernate, struts, and so on.
  4. How to use clone: Whenever we invoke the Clone method of an object, the JVM creates a new object that copies the contents of the previous object, and creates the object with the Clone method without invoking any constructors. To use the Clone method, we must first implement the Cloneable interface and implement its definition of the Clone method. such as: Student STU2 = <student>stu.clone (); This is also the application of prototype mode.
  5. Use deserialization: When we serialize and deserialize an object, the JVM creates a separate object for us, and when deserializing, the JVM creates the object and does not call any constructors. In order to deserialize an object, we need to have our class implement the Serializable interface. such as: ObjectInputStream in = new ObjectInputStream (New FileInputStream ("Data.obj")); Student stu3 = (Student) in.readobject ();

As can be seen from the above example, except that all methods other than using the New keyword are converted to invokevirtual (the direct method of creating the object), using the new method to convert to two calls, new and invokespecial (constructor call).

Also: Are there any differences between the two newinstance methods?

The ☆class class is in the Lang packet of Java, and the constructor class is part of the Java reflection mechanism.

The newinstance of the ☆class class can only trigger a parameterless construction method to create an object, and the newinstance of the constructor class can trigger a constructor with arguments or arbitrary arguments to create the object.

The newinstance of the ☆class class requires that its construction method be common or visible to the calling method, whereas the newinstance of the constructor class can invoke the private construction method to create the object under a specific environment.

The newinstance of the ☆class class throws an exception to the class constructor, and the newinstance of the constructor class wraps a InvocationTargetException exception.

Class classes essentially invoke the Newinstance method with no parameters in the reflection packet constructor class, capturing the invocationtargetexception and throwing the exception from the constructor itself.

Several ways to create objects 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.