java several ways to create objects and create objects of the process

1. Create an object five ways

package com.linyf.demo;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

/**
 * 创建对象的五种方式
 */
public class ObjectCreateTest implements Cloneable{
    public static void main(String[] args) throws IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException, CloneNotSupportedException, IOException, ClassNotFoundException {
        // 1. 使用new 关键字创建
        Object o = new Object();

        // 2. 使用反射
        Object o1 = Object.class.newInstance();

        // 3. 使用Constructor
        Constructor<Object> constructor = Object.class.getConstructor();
        Object o2 = constructor.newInstance();

        // 4. clone 需要实现实现Cloneable接口 ----------不常用
        ObjectCreateTest objectCreateTest = new ObjectCreateTest();
        ObjectCreateTest clone = (ObjectCreateTest)objectCreateTest.clone();

        // 5. 反序列化 ------------不常用
        ObjectInputStream in = new ObjectInputStream(new FileInputStream("data.obj"));
        ObjectCreateTest seriaObj = (ObjectCreateTest) in.readObject();
    }
}

2. The process of creating an object

To create a common Java objects, to the following procedure:
1, virtual opportunities to new instructions to locate the symbol constant pool of the class reference.
2, to check whether the class represented by the symbolic references is loaded, parsed, initialized.
3, virtual machine memory allocated for the object.
4, the virtual machine memory space assigned to all initialized to zero values.
5, the virtual machine to target the necessary settings.
6, the implementation methods, member variables are initialized.

Published 11 original articles · won praise 0 · Views 610

Guess you like

Origin blog.csdn.net/fei1234456/article/details/105168302