Java创建对象的方式

显式创建对象

有4种显式地创建对象的方式:

  1. 用new语句创建对象,这是最常用的创建对象的方式。
  2. 运用反射手段,调用Java.lang.Class或者java.lang.reflect.Constructor类的newInstance()实例方法。
  3. 调用对象的clone()方法。
  4. 运用反序列化手段,调用java.io.ObjectInputStream对象的readObject()方法。

下面演示了用前面3种方式创建对象的过程:

public class Customer implements Cloneable{  
  private String name;  
  private int age;  

  public Customer(){  
    this("unknown",0);  
    System.out.println("call default constructor");  
  }  

  public Customer(String name,int age){  
    this.name=name;  
    this.age=age;  
    System.out.println("call second constructor");  
  }  

  public Object clone()throws CloneNotSupportedException{  
    return super.clone();  
  }  

  public boolean equals(Object o){  
    if(this==o) return true;  
    if(!(o instanceof Customer)) return false;  
    final Customer other=(Customer)o;  
    if(this.name.equals(other.name) && this.age==other.age)  
       return true;  
    else  
       return false;  
  }  

  public String toString(){  
    return "name="+name+",age="+age;  
  }

  public static void main(String args[])throws Exception{  
   //运用反射手段创建Customer对象  
   Class objClass=Class.forName("Customer");  
   Customer c1=(Customer)objClass.newInstance(); //会调用Customer类的默认构造方法  
   System.out.println("c1: "+c1); //打印name=unknown,age=0  
   
   //用new语句创建Customer对象  
   Customer c2=new Customer("Tom",20);  
   System.out.println("c2: "+c2); //打印name=tom,age=20  
   
   //运用克隆手段创建Customer对象  
   Customer c3=(Customer)c2.clone(); //不会调用Customer类的构造方法  
   System.out.println("c2==c3 : "+(c2==c3)); //打印false  
   System.out.println("c2.equals(c3) : "+c2.equals(c3)); //打印true  
   System.out.println("c3: "+c3); //打印name=tom,age=20  
  }  
}  

从以上打印结果看出,用new语句或Class对象的newInstance()方法创建Customer对象时,都会执行Customer类的构造方法,而用对象的clone()方法创建Customer对象时,不会执行Customer类的构造方法。(区别)

newInstance()方法

  1. 使用newInstance的前提是,类已加载并且这个类已连接,这是正是class的静态方法forName()完成的工作。newInstance实际上是把new这个方式分解为两步,即,首先调用class的加载方法加载某个类,然后实例化。
  2. newInstance:弱类型,低效率,只能调用无参构造。 new:强类型。相对高效,能调用任何public构造。
  3. newInstance()是实现IOC、反射、面对接口编程和依赖倒置等技术方法的必然选择,new只能实现具体类的实例化,不适合于接口编程。
  4. newInstance() 一般用于动态加载类。

隐式创建对象

除了以上4种显式地创建对象的方式以外,在程序中还可以隐含地创建对象,包括以下几种情况:

(1)对于java命令中的每个命令行参数,Java虚拟机都会创建相应的String对象,并把它们组织到一个String数组中,再把该数组作为参数传给程序入口main(String args[])方法。

(2)程序代码中的String类型的直接数对应一个String对象,例如:

String s1="Hello";  
String s2="Hello"; //s2和s1引用同一个String对象  
String s3=new String("Hello");  
System.out.println(s1==s2); //打印true  
System.out.println(s1==s3); //打印false  

执行完以上程序,内存中实际上只有两个String对象,一个是直接数,由Java虚拟机隐含地创建,还有一个通过new语句显式地创建。

(3)字符串操作符“+”的运算结果为一个新的String对象。例如:

String s1="H";  
String s2="ello";  
String s3=s1+s2; //s3引用一个新的String对象  
System.out.println(s3=="Hello"); //打印false  
System.out.println(s3.equals("Hello")); //打印true  

(4)当Java虚拟机加载一个类时,会隐含地创建描述这个类的Class实例。

你可能感兴趣的:(Java创建对象的方式)