今天总结一下,在Java中,创建对象可以有几种花样。
① new 关键字
这是最常用的方式了,这种方式是通过调用构造器来初始化对象及实例字段的。每个类的构造器皆会直接或间接调用父类的构造器,并且在同一个实例中初始化相应的字段。
注意,调用构造器时,如果继承自父类,要显式用 super() 关键字或隐式用 this 关键字传入父类构造器的参数。
② 反射机制
这种方式也是通过反射方法,调用构造器来初始化对象。
③ Object.clone()
这种方式直接复制已有的数据,来初始化新建对象的实例字段,当然,包含浅克隆和深克隆。
④ 反序列化
与 ③ 原理相同。
⑤ Unsafe.allocateInstance()
此方法为 native 方法,没有初始化实例字段。它只做了分配内存空间,返回内存地址,并没有调用构造函数。所以 Unsafe.allocateInstance() 方法创建的对象都是只有初始值,没有默认值也没有构造函数设置的值,因为它完全没有使用 new 机制,直接操作内存创建了对象。
import java.lang.reflect.Field;
import sun.misc.Unsafe;
public class UnsafeUtility {
private static Unsafe unsafe;
static {
try {
Field f = Unsafe.class.getDeclaredField("theUnsafe");
f.setAccessible(true);
unsafe = (Unsafe) f.get(null);
} catch (Exception e) {
}
}
public static Unsafe getUnsafe(){
return unsafe;
}
}
以下为测试用例:
public class DemoClass {
private int value1;
private int value2 = 10;
public DemoClass(int value1, int value2){
this.value1 = value1;
this.value2 = value2;
}
public int getValue1() {
return value1;
}
public void setValue1(int value1) {
this.value1 = value1;
}
public int getValue2() {
return value2;
}
public void setValue2(int value2) {
this.value2 = value2;
}
}
import java.lang.reflect.Constructor;
import java.lang.reflect.Type;
import sun.misc.Unsafe;
import com.zc.lock.UnsafeUtility;
public class Main {
public static void main(String[] args){
// testNewObject();
// testNewInstance();
// testConstructor();
// testConstructorWityParameterTypes();
// testUnsafeAllocateInstance();
}
public static void testUnsafeAllocateInstance(){
Unsafe unsafe = UnsafeUtility.getUnsafe();
try {
DemoClass obj = (DemoClass)unsafe.allocateInstance(DemoClass.class);
System.out.println(obj.getValue1());
System.out.println(obj.getValue2());
obj.setValue1(1);
obj.setValue2(2);
System.out.println(obj.getValue1());
System.out.println(obj.getValue2());
} catch (InstantiationException e) {
e.printStackTrace();
}
}
public static void testNewObject(){
DemoClass obj = new DemoClass(1,2);
System.out.println(obj.getValue1());
System.out.println(obj.getValue2());
}
public static void testNewInstance(){
try {
DemoClass obj = DemoClass.class.newInstance();
System.out.println(obj.getValue1());
System.out.println(obj.getValue2());
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
public static void testConstructor(){
try {
Class[] cls = new Class[] { int.class, int.class };
Constructor c = DemoClass.class.getDeclaredConstructor(cls);
DemoClass obj = (DemoClass) c.newInstance(0, 0);
System.out.println(obj.getValue1());
System.out.println(obj.getValue2());
} catch (Exception e) {
e.printStackTrace();
}
}
public static void testConstructorWityParameterTypes(){
try {
Constructor[] c = DemoClass.class.getDeclaredConstructors();
Type[] parameterTypes = c[0].getGenericParameterTypes();
// 判断type类型,依次设置默认值
DemoClass obj = (DemoClass) c[0].newInstance(0, 0);
System.out.println(obj.getValue1());
System.out.println(obj.getValue2());
} catch (Exception e) {
e.printStackTrace();
}
}
}