做爲Java開發者,咱們天天都會建立大量的對象,可是,咱們老是使用管理依賴系統(如Spring框架)來建立這些對象。其實還有其餘方法能夠建立對象,在接下來的文章中我會進行詳細介紹。html
這是最多見的建立對象的方法,而且也很是簡單。經過使用這種方法咱們能夠調用任何咱們須要調用的構造函數。java
Employee emp1 = new Employee();
0: new #19 // class org/programming/mitra/exercises/Employee 3: dup 4: invokespecial #21 // Method org/programming/mitra/exercises/Employee."":()V
咱們也可使用class類的newInstance()方法來建立對象。此newInstance()方法調用無參構造函數以建立對象。框架
咱們能夠經過newInstance() 用如下方式建立對象:ide
Employee emp2 = (Employee) Class.forName("org.programming.mitra.exercises.Employee").newInstance();
或者函數
Employee emp2 = Employee.class.newInstance();
51: invokevirtual #70 // Method java/lang/Class.newInstance:()Ljava/lang/Object;
與使用class類的newInstance()方法類似,java.lang.reflect.Constructor類中有一個能夠用來建立對象的newInstance()函數方法。經過使用這個newInstance()方法咱們也能夠調用參數化構造函數和私有構造函數。this
Constructor<Employee> constructor = Employee.class.getConstructor(); Employee emp3 = constructor.newInstance();
111: invokevirtual #80 // Method java/lang/reflect/Constructor.newInstance:([Ljava/lang/Object;)Ljava/lang/Object;
這些 newInstance() 方法被認爲是建立對象的反射手段。實際上,內部類的newInstance()方法使用構造函數類的 newInstance() 方法。這就是爲何後者是首選而且使用不一樣的框架如Spring, Hibernate, Struts等。code
實際上不管什麼時候咱們調用clone() 方法,JAVA虛擬機都爲咱們建立了一個新的對象而且複製了以前對象的內容到這個新的對象中。使用 clone()方法建立對象不會調用任何構造函數。htm
爲了在對象中使用clone()方法,咱們須要在其中實現可克隆類型並定義clone()方法。對象
Employee emp4 = (Employee) emp3.clone();
162: invokevirtual #87 // Method org/programming/mitra/exercises/Employee.clone ()Ljava/lang/Object;
不管什麼時候咱們對一個對象進行序列化和反序列化,JAVA虛擬機都會爲咱們建立一個單獨的對象。在反序列化中,JAVA虛擬機不會使用任何構造函數來建立對象。接口
對一個對象進行序列化須要咱們在類中實現可序列化的接口。
ObjectInputStream in = new ObjectInputStream(new FileInputStream("data.obj")); Employee emp5 = (Employee) in.readObject();
261: invokevirtual #118 // Method java/io/ObjectInputStream.readObject:()Ljava/lang/Object;
正如咱們在以上的字節代碼片斷中所看到的,除第一種被轉換爲一個新的函數和一個 invokespecial 指令之外,其它4種方法都被調用並轉換爲invokevirtual。
讓咱們來看看準備建立對象的 Employee 類:
class Employee implements Cloneable, Serializable { private static final long serialVersionUID = 1L; private String name; public Employee() { System.out.println("Employee Constructor Called..."); } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((name == null) ? 0 : name.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Employee other = (Employee) obj; if (name == null) { if (other.name != null) return false; } else if (!name.equals(other.name)) return false; return true; } @Override public String toString() { return "Employee [name=" + name + "]"; } @Override public Object clone() { Object obj = null; try { obj = super.clone(); } catch (CloneNotSupportedException e) { e.printStackTrace(); } return obj; } }
在下面的Java程序中咱們用5種方式來建立 Employee對象。
public class ObjectCreation { public static void main(String... args) throws Exception { // By using new keyword Employee emp1 = new Employee(); 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 = new ObjectOutputStream(new FileOutputStream("data.obj")); out.writeObject(emp4); out.close(); //Deserialization ObjectInputStream in = new ObjectInputStream(new FileInputStream("data.obj")); Employee emp5 = (Employee) in.readObject(); in.close(); emp5.setName("Akash"); System.out.println(emp5 + ", hashcode : " + emp5.hashCode()); } }
此程序輸出結果以下:
Employee Constructor Called... Employee [name=Naresh], hashcode : -1968815046 Employee Constructor Called... Employee [name=Rishi], hashcode : 78970652 Employee Constructor Called... Employee [name=Yogesh], hashcode : -1641292792 Employee [name=Atul], hashcode : 2051657 Employee [name=Akash], hashcode : 63313419
本文譯自:Dzone 譯者:慧都控件網-慧都小藝