【Java基础】Java对象创建的几种方式
创始人
2024-05-26 06:35:22
0

先上关键内容,所用到的代码请参考文末示例代码。

一、使用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();

四、使用克隆clone()方法创建对象

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

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

Student student4 = new Student().clone();

五、使用反序列化创建对象

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;@Overridepublic Student clone() {try {Student clone = (Student) super.clone();// TODO: copy mutable state here, so the clone can't change the internals of the originalreturn 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);}}
}

完结!

相关内容

热门资讯

资本界金控(00204.HK)... 格隆汇7月2日丨资本界金控(00204.HK)公告,公司近期与江苏福万代科技信息有限公司("福万代"...
新希望:独立董事彭龙接受纪律审... 转自:财联社【新希望:独立董事彭龙接受纪律审查和监察调查】财联社7月2日电,新希望公告,公司独立董事...
3岁萌娃迷路街头,妈妈急疯!两... 来源:杭州公安 “你好 我在天万街与羊头坝路交叉口的桥上 这边有个小孩子找不到家人一直在哭” 6月2...
中考成绩单|低波策略业绩盘点 (转自:国泰基金微幸福)
华菱钢铁:投资者建议连续三年回... 投资者提问:回购注销是财务手段里提升pb最稳定最可靠最有效的方式,在pb回升到1之前,每年分红可以少...
今年前5个月销量排名前十位轿车... 转自:北京商报北京商报讯(记者 刘晓梦)7月2日,据中国汽车工业协会统计分析,今年前5个月,销量排名...
法国已有2人死于热浪 转自:新华社新华社巴黎7月2日电(记者罗毓)法国商业调频电视台2日上午报道,最近该国遭遇强热浪天气,...
赛意信息(300687.SZ)... 格隆汇7月2日丨赛意信息(300687.SZ)公布,截至2025年6月30日,公司通过股份回购专用证...
比亚迪汽车:海洋网累计销售超5... 7月2日,比亚迪汽车官方微博显示,比亚迪海洋网6月热销196766辆,同比增长24.6%。2025年...
能特科技:拟以3亿元-5亿元回... 格隆汇7月2日|能特科技公告,公司拟使用自有资金及自筹资金,本次回购股份将全部用于注销并相应减少注册...