Java 中创建对象的 5 种方式!

640?wx_fmt=gif

640?wx_fmt=jpeg

看什么看!点我呀! 全栈程序员,免费入门到精通! 640?wx_fmt=jpeg


作者丨java技术栈

codeceo.com/article/5-ways-java-create-object.html


作为Java开发者,我们每天创建很多对象,但我们通常使用依赖管理系统,比如Spring去创建对象。然而这里有很多创建对象的方法,我们会在这篇文章中学到。


Java中有5种创建对象的方式,下面给出它们的例子还有它们的字节码。


640?


如果你运行了末尾的的程序,你会发现方法1,2,3用构造函数创建对象,方法4,5没有调用构造函数。


640?wx_fmt=gif1使用new关键字


这是最常见也是最简单的创建对象的方式了。通过这种方式,我们可以调用任意的构造函数(无参的和带参数的)。


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


640?wx_fmt=gif2使用Class类的newInstance方法


我们也可以使用Class类的newInstance方法创建对象。这个newInstance方法调用无参的构造函数创建对象。


我们可以通过下面方式调用newInstance方法创建对象:


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;


640?wx_fmt=gif3使用Constructor类的newInstance方法


和Class类的newInstance方法很像, java.lang.reflect.Constructor类里也有一个newInstance方法可以创建对象。


我们可以通过这个newInstance方法调用有参数的和私有的构造函数。大家也可以看下

https://mp.weixin.qq.com/s/3u7dTh-TLA8fVlRvfw9BaA


Constructor<Employeeconstructor = Employee.class.getConstructor();
Employee emp3 = constructor.newInstance();


111: invokevirtual  #80  // Method java/lang/reflect/Constructor.newInstance:([Ljava/lang/Object;)Ljava/lang/Object;


newInstance方法内部调用Constructor的newInstance方法。这也是众多框架,如Spring、Hibernate、Struts等使用后者的原因。


640?wx_fmt=gif4使用clone方法


无论何时我们调用一个对象的clone方法,jvm就会创建一个新的对象,将前面对象的内容全部拷贝进去。用clone方法创建对象并不会调用任何构造函数。


要使用clone方法,我们需要先实现Cloneable接口并实现其定义的clone方法。


Employee emp4 = (Employee) emp3.clone();’


162: invokevirtual #87  // Method org/programming/mitra/exercises/Employee.clone ()Ljava/lang/Object;


640?wx_fmt=gif5使用反序列化


当我们序列化和反序列化一个对象,jvm会给我们创建一个单独的对象。在反序列化时,jvm创建对象并不会调用任何构造函数。


为了反序列化一个对象,我们需要让我们的类实现Serializable接口。


ObjectInputStream in = new ObjectInputStream(new FileInputStream("data.obj"));
Employee emp5 = (Employee) in.readObject();


261: invokevirtual  #118   
// Method java/io/ObjectInputStream.readObject:()Ljava/lang/Object;


我们从上面的字节码片段可以看到,除了第1个方法,其他4个方法全都转变为invokevirtual(创建对象的直接方法),第一个方法转变为两个调用,new和invokespecial(构造函数调用)。


640?wx_fmt=gif例子


让我们看一看为下面这个Employee类创建对象:


class Employee implements CloneableSerializable {   
   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对象。你可以从GitHub找到这些代码。


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());   
   }   
}


程序会输出:


public class ObjectCreation {   
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


你还知道别的吗?


 推荐↓↓↓ 

640?wx_fmt=png

?16个技术公众号】都在这里!

涵盖:程序员大咖、源码共读、程序员共读、数据结构与算法、黑客技术和网络安全、大数据科技、编程前端、Java、Python、Web编程开发、Android、iOS开发、Linux、数据库研发、幽默程序员等。

觉得我们“好看”的点亮它~
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值