1. new 关键字 创建
2. 反射机制(Class.forName("完整类名"))
优点:使用灵活,可以不修改代码,通过修改配置文件来任意的创建对象
如果只希望一个类的静态代码执行,其他代码不执行,就可使用反射机制
例:Class.forName("com.mysql.cj.jdbc.Driver");
public class Main {
public static void main(String[] args) throws Exception {
Class<Person> clazz = (Class<Person>) Class.forName("Person");
Person person = clazz.newInstance();
System.out.println(person);
}
}
注:使用newInstance创建类时,被创建的类中必须存在无参构造否则会报错
Exception in thread "main" java.lang.InstantiationException: Person
at java.lang.Class.newInstance(Class.java:427)
at Main.main(Main.java:9)
Caused by: java.lang.NoSuchMethodException: Person.<init>()
at java.lang.Class.getConstructor0(Class.java:3082)
at java.lang.Class.newInstance(Class.java:412)
... 1 more
3.Constructors.Instance()
比Class.forName()的功能更加强大,可以使用它调用一些私有的构造方法,而不仅仅限制于public
3.1 getDeclaredConstructors()
public class Main {
public static void main(String[] args) throws Exception {
Constructor<?>[] constructors = Person.class.getDeclaredConstructors();
System.out.println(constructors[0]);
System.out.println(constructors[1]);
Person person = (Person) constructors[0].newInstance();
System.out.println(person);
Person person1 = (Person) constructors[1].newInstance("111");
System.out.println(person1);
}
}
public Person()
public Person(java.lang.String)
Person{name='null'}
Person{name='111'}
public class Person {
String name;
public Person() {
}
public Person(String name) {
this.name = name;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
'}';
}
}
注:constructors数组中构造参数的下标取决与被创建类中构造方法的前后顺序
3.2 getConstructors()
此方法只能得到public的构造方法,得到的只能是getDeclaredConstructors()结果的子集
public class Main {
public static void main(String[] args) {
Constructor<?>[] constructors = Person.class.getDeclaredConstructors();
Constructor<?>[] constructors1 = Person.class.getConstructors();
System.out.println(constructors.length);
System.out.println(constructors[0]);
System.out.println(constructors[1]);
System.out.println(constructors1.length);
System.out.println(constructors1[0]);
}
}
public class Person {
String name;
public Person() {
}
private Person(String name) {
this.name = name;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
'}';
}
}
4. Clone
若想使用此方法,则必须实现Cloneable接口并重写其中的clone方法,clone()方法是浅拷贝,不会调用构造方法,只会将被拷贝的类中的属性的地址引用到新的类中
public class People implements Cloneable {
public People() {
System.out.println("我被调用了");
}
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
public static void main(String[] args) throws CloneNotSupportedException {
People people = new People();
People clone = (People) people.clone();
System.out.println(people.equals(clone));
}
}
5. 反序列化
被序列化的类必须实现Serializable接口
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.9</version>
</dependency>
public class People implements Serializable {
String name;
int age;
public People(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "People{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
public static void main(String[] args) {
People people = new People("li",11);
byte[] bytes = SerializationUtils.serialize(people);
Object people1 = SerializationUtils.deserialize(bytes);
System.out.println(people);
System.out.println(people1);
System.out.println(people.equals(people1));
}
}
在单例模式中,可以定义readResolve方法实现单例模式防止反序列化打破单例。当jvm从内存中反序列化地"组装"一个新对象时,会自动调用这个 readResolve方法来返回我们指定好的对象了,单例规则也就得到了保证。
private object readResolve() throws objectstreamexception {
return instance;
}
至于这个方法readResolve在类ObjectInputStream中会检测对象是否存在,而调用