作为Java开发者,我们每天都会创建大量的对象,但是,我们总是使用管理依赖系统(如Spring框架)来创建这些对象。其实还有其他方法可以创建对象,在接下来的文章中我会进行详细介绍。
这是最常见的创建对象的方法,并且也非常简单。通过使用这种方法我们可以调用任何我们需要调用的构造函数。
| 1 | Employee emp1 =?newEmployee(); | 
| 1 2 3 | 0:?new??????????#19?????????// class org/programming/mitra/exercises/Employee3: dup4: invokespecial #21?????????// Method org/programming/mitra/exercises/Employee."":()V | 
我们也可以使用class类的newInstance()方法来创建对象。此newInstance()方法调用无参构造函数以创建对象。
我们可以通过newInstance() 用以下方式创建对象:
| 1 | Employee emp2 = (Employee) Class.forName("org.programming.mitra.exercises.Employee").newInstance(); | 
或者
| 1 | Employee emp2 = Employee.class.newInstance(); | 
| 1 | 51: invokevirtual??? #70???// Method java/lang/Class.newInstance:()Ljava/lang/Object; | 
与使用class类newInstance()方法相似,java.lang.reflect.Constructor类中有一个可以用来创建对象的newInstance()函数方法。通过使用这个newInstance()方法我们也可以调用参数化构造函数和私有构造函数。
| 1 2 | Constructor<employee> constructor = Employee.class.getConstructor();Employee emp3 = constructor.newInstance();</employee> | 
| 1 | 111: invokevirtual? #80?// Method java/lang/reflect/Constructor.newInstance:([Ljava/lang/Object;)Ljava/lang/Object; | 
这些 newInstance() 方法被认为是创建对象的反射手段。实际上,内部类的newInstance()方法使用构造函数类的 newInstance() 方法。这就是为什么后者是首选并且使用不同的框架如Spring, Hibernate, Struts等。
实际上无论何时我们调用clone() 方法,JAVA虚拟机都为我们创建了一个新的对象并且复制了之前对象的内容到这个新的对象中。使用 clone()方法创建对象不会调用任何构造函数。
为了在对象中使用clone()方法,我们需要在其中实现可克隆类型并定义clone()方法。
| 1 | Employee emp4 = (Employee) emp3.clone(); | 
| 1 | 162: invokevirtual #87?// Method org/programming/mitra/exercises/Employee.clone ()Ljava/lang/Object; | 
无论何时我们对一个对象进行序列化和反序列化,JAVA虚拟机都会为我们创建一个单独的对象。在反序列化中,JAVA虚拟机不会使用任何构造函数来创建对象。
对一个对象进行序列化需要我们在类中实现可序列化的接口。
| 1 2 | ObjectInputStream in =?newObjectInputStream(newFileInputStream("data.obj"));Employee emp5 = (Employee) in.readObject(); | 
| 1 | 261: invokevirtual? #118??// Method java/io/ObjectInputStream.readObject:()Ljava/lang/Object; | 
正如我们在以上的字节代码片段中所看到的,除第一种被转换为一个新的函数和一个 invokespecial 指令以外,其它4种方法都被调用并转换为invokevirtual。
让我们来看看准备创建对象的 Employee 类:
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | classEmployee?implementsCloneable, Serializable {????privatestaticfinallongserialVersionUID = 1L;????privateString name;????publicEmployee() {????????System.out.println("Employee Constructor Called...");????}????publicString getName() {????????returnname;????}????publicvoidsetName(String name) {????????this.name = name;????}????@Override????publicinthashCode() {????????finalintprime =?31;????????intresult =?1;????????result = prime * result + ((name ==?null) ??0: name.hashCode());????????returnresult;????}????@Override????publicbooleanequals(Object obj) {????????if(this== obj)????????????returntrue;????????if(obj ==?null)????????????returnfalse;????????if(getClass() != obj.getClass())????????????returnfalse;????????Employee other = (Employee) obj;????????if(name ==?null) {????????????if(other.name !=?null)????????????????returnfalse;????????}?elseif(!name.equals(other.name))????????????returnfalse;????????returntrue;????}????@Override????publicString toString() {????????return"Employee [name="+ name +?"]";????}????@Override????publicObject clone() {????????Object obj =?null;????????try{????????????obj =?super.clone();????????}?catch(CloneNotSupportedException e) {????????????e.printStackTrace();????????}????????returnobj;????}} | 
在下面的Java程序中我们用5种方式来创建 Employee对象。
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | publicclassObjectCreation {????publicstaticvoidmain(String... args)?throwsException {????????// By using new keyword????????Employee emp1 =?newEmployee();????????emp1.setName("Naresh");????????System.out.println(emp1 +?", hashcode : "+ emp1.hashCode());????????// By using Class class‘s newInstance() method????????Employee emp2 = (Employee) Class.forName("org.programming.mitra.exercises.Employee")???????????????????????????????.newInstance();????????// Or we can simply do this????????// Employee emp2 = Employee.class.newInstance();????????emp2.setName("Rishi");????????System.out.println(emp2 +?", hashcode : "+ emp2.hashCode());????????// By using Constructor class‘s newInstance() method????????Constructor<employee> constructor = Employee.class.getConstructor();????????Employee emp3 = constructor.newInstance();????????emp3.setName("Yogesh");????????System.out.println(emp3 +?", hashcode : "+ emp3.hashCode());????????// By using clone() method????????Employee emp4 = (Employee) emp3.clone();????????emp4.setName("Atul");????????System.out.println(emp4 +?", hashcode : "+ emp4.hashCode());????????// By using Deserialization????????// Serialization????????ObjectOutputStream out =?newObjectOutputStream(newFileOutputStream("data.obj"));????????out.writeObject(emp4);????????out.close();????????//Deserialization????????ObjectInputStream in =?newObjectInputStream(newFileInputStream("data.obj"));????????Employee emp5 = (Employee) in.readObject();????????in.close();????????emp5.setName("Akash");????????System.out.println(emp5 +?", hashcode : "+ emp5.hashCode());????}}</employee> | 
此程序输出结果如下:
| 1 2 3 4 5 6 7 8 | Employee Constructor Called...Employee [name=Naresh], hashcode : -1968815046Employee Constructor Called...Employee [name=Rishi], hashcode :?78970652Employee Constructor Called...Employee [name=Yogesh], hashcode : -1641292792Employee [name=Atul], hashcode :?2051657Employee [name=Akash], hashcode :?63313419 | 
本文译自:Dzone
原文:http://619388112.iteye.com/blog/2312211