Java创建对象的五种方式
1. 使用new关键字
Object obj = new Object();
2.使用反射的Class类的newInstance()创建
Class cla = Class.forName("全类名");
Object obj = cla.newInstance();
3.使用反射的Constructor类的newInstance()创建
Class cla = Object.class();
Constructor constructor = cla.getConstructor();
Object obj = constructor.newInstance();
4.使用clone()方法
Object obj2 = obj1.clone();
5.使用反序列化(ObjectInputStream)的readObject()方法
try (ObjectInputStream ois
= new ObjectInputStream(new FileInputStream(FILE_NAME))) {
ObjectName obj = ois.readObject();
}