先上关键内容,所用到的代码请参考文末示例代码。
一、使用new关键字创建对象
这是一种最常用的创建对象的方式。
student student1 = new student();
二、使用class的newinstance()方法创建对象
需要有一个无参构造方法,这个newinstance()方法调用无参的构造函数创建对象。
类名.calss.newinstance( )
student student2 = student.class.newinstance();
该方法就是反射机制,事实上class的newinstance()方法内部就是调用constructor的newinstance()方法。
class类的newinstance只能触发无参构造方法创建对象,而构造器类的newinstance能触发有参数或者任意参数的构造方法来创建对象。
三、使用constructor类的newinstance()方法创建对象
java.lang.reflect.constructor类里也有一个newinstance()方法可以创建对象。我们可以通过这个newinstance()方法调用有参数的和私有的构造函数。
constructor student3 = constructor.class.newinstance();
五、使用反序列化创建对象
java序列化是指把java对象转换为字节序列的过程,而java反序列化是指把字节序列恢复为java对象的过程;
使用反序列化:当我们序列化和反序列化一个对象,jvm会给我们创建一个单独的对象。在反序列化时,jvm创建对象并不会调用任何构造函数。
为了反序列化一个对象,我们需要让我们的类实现serializable接口。
objectinputstream ois = new objectinputstream(new fileinputstream(file_name)); // 5、使用反序列化创建对象 object student5 = ois.readobject();
六、创建对象的5种方式调用构造器总结
创建对象的方式 | 是否调用了构造器 |
使用new关键字创建对象 | 是 |
class.newinstance() | 是 |
constructor.newinstance() | 是 |
clone() | 否 |
反序列化 | 否 |
java创建实例对象是不是必须要通过构造函数?这其实是衍生出来的一个面试题。 上面问题的答案很明显了:java创建实例对象,并不一定必须要调用构造器的。
七、示例代码(全)
以下是本文所用到的所有示例代码。
7.1 编写student学生类
package com.effective.chapter2.other;
import lombok.allargsconstructor;
import lombok.data;
import lombok.noargsconstructor;
import java.io.serializable;
@data
@allargsconstructor
@noargsconstructor
public class student implements cloneable, serializable {
private string name;
private integer age;
@override
public student clone() {
try {
student clone = (student) super.clone();
// todo: copy mutable state here, so the clone can't change the internals of the original
return clone;
} catch (clonenotsupportedexception e) {
throw new assertionerror();
}
}
}7.2 编写测试类
package com.effective.chapter2.other;
import java.io.*;
import java.lang.reflect.constructor;
public class createobjecttest {
private static final string file_name = "student.obj";
public static void main(string[] args) throws instantiationexception, illegalaccessexception, ioexception {
// 1、使用new关键字创建对象
student student1 = new student();
system.out.println("使用new关键字创建对象:" + student1);
// 2、使用class类的newinstance()方法创建对象
student student2 = student.class.newinstance();
system.out.println("使用class类的newinstance()方法创建对象:" + student2);
// 3、使用constructor类的newinstance()方法创建对象
constructor student3 = constructor.class.newinstance();
system.out.println("使用constructor类的newinstance()方法创建对象:" + student3);
// 4、使用clone()方法创建对象
student student4 = student2.clone();
system.out.println("使用clone()方法创建对象:" + student4);
try {
// java序列化是指把java对象转换为字节序列的过程,而java反序列化是指把字节序列恢复为java对象的过程;
// 序列化对象
objectoutputstream oos = new objectoutputstream(new fileoutputstream(file_name));
oos.writeobject(student1);
objectinputstream ois = new objectinputstream(new fileinputstream(file_name));
// 5、使用反序列化创建对象
object student5 = ois.readobject();
system.out.println("使用反序列化创建对象:" + student5);
} catch (classnotfoundexception e) {
throw new runtimeexception(e);
}
}
}到此这篇关于java对象创建的几种方式总结的文章就介绍到这了,更多相关java对象创建方式内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论