SoFunction
Updated on 2025-04-06

Summary of several ways of creating Java objects

First, please refer to the example code at the end of the article.

1. Create an object using the new keyword

This is the most commonly used way to create objects.

Student student1 = new Student();

2. Create an object using Class's newInstance() method

There is a parameterless constructor method, and this newInstance() method calls the parameterless constructor to create an object.

Class name.( )

Student student2 = ();

This method is the reflection mechanism. In fact, the newInstance() method of Class is called the newInstance() method of the Constructor.

The newInstance of the Class class can only trigger the creation of an object without parameter construction method, while the newInstance of the constructor class can trigger the construction method with parameters or arbitrary parameters to create an object.

3. Create an object using the newInstance() method of the Constructor class

There is also a newInstance() method in the class to create objects. We can call parameterized and private constructors through this newInstance() method.

Constructor student3 = ();

5. Create an object using deserialization

Java serialization refers to the process of converting Java objects into byte sequences, while Java deserialization refers to the process of restoring the byte sequences into Java objects;

Using Deserialization: When we serialize and deserialize an object, jvm will create a separate object for us. During deserialization, jvm creates the object and does not call any constructor.

In order to deserialize an object, we need to have our class implement the Serializable interface.

ObjectInputStream ois = new ObjectInputStream(new FileInputStream(FILE_NAME));
// 5. Create an object using deserializationObject student5 = ();

6. Summary of 5 ways to create objects to call the constructor

How to create objects

Whether the constructor is called

Create an object using the new keyword

yes

()

yes

()

yes

clone()

no

Deserialization

no

Does Java need to use constructors to create instance objects? This is actually a derived interview question. The answer to the above question is obvious: Java does not necessarily have to call the constructor to create an instance object.

7. Sample code (full)

Here are all the sample codes used in this article.

7.1 Write Student Student Class

package .;
 
import ;
import ;
import ;
 
import ;
 
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Student implements Cloneable, Serializable {
    private String name;
    private Integer age;
 
    @Override
    public Student clone() {
        try {
            Student clone = (Student) ();
            // TODO: copy mutable state here, so the clone can't change the internals of the original
            return clone;
        } catch (CloneNotSupportedException e) {
            throw new AssertionError();
        }
    }
}

7.2 Write test classes

package .;
 
import .*;
import ;
 
public class CreateObjectTest {
 
    private static final String FILE_NAME = "";
 
    public static void main(String[] args) throws InstantiationException, IllegalAccessException, IOException {
 
        // 1. Create an object using the new keyword        Student student1 = new Student();
        ("Create an object using the new keyword:" + student1);
 
        // 2. Create an object using the newInstance() method of the Class class        Student student2 = ();
        ("useClassClassicnewInstance()Methods to create objects:" + student2);
 
        // 3. Create an object using the newInstance() method of the Constructor class        Constructor student3 = ();
        ("useConstructorClassicnewInstance()Methods to create objects:" + student3);
 
        // 4. Use the clone() method to create an object        Student student4 = ();
        ("useclone()Methods to create objects:" + student4);
 
        try {
            // Java serialization refers to the process of converting Java objects into byte sequences, while Java deserialization refers to the process of restoring the byte sequences into Java objects;            // Serialize objects            ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(FILE_NAME));
            (student1);
 
            ObjectInputStream ois = new ObjectInputStream(new FileInputStream(FILE_NAME));
            // 5. Create an object using deserialization            Object student5 = ();
            ("Create an object using deserialization:" + student5);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }
}

This is the end of this article about the summary of several ways of creating Java object. For more related content on Java object, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!