Look at the code
public class Main {
public static void main(String[] args) {
Person person1 = new Person();
PersonWrapper person1Wrapper = new PersonWrapper(person1);
System.out.println(person1.toString());// Person@4554617c
System.out.println(person1Wrapper.person.toString());// Person@4554617c
//分别改变包装类和本类的对象引用
changePerson(person1);
changePersonWrapper(person1Wrapper);
System.out.println(person1.toString());// Person@4554617c
System.out.println(person1Wrapper.person.toString());//Person@74a14482 指针发生了改变
}
private static void changePersonWrapper(PersonWrapper personWrapper) {
personWrapper.person = new Person();
}
private static void changePerson(Person person){
person = new Person();
}
}
class Person{}
//包装Person类
class PersonWrapper{
public Person person;
public PersonWrapper(Person person){
this.person = person;
}
}
Here we are by implementing a wrapper class for the Person itself, to implement changes to the internal object fields and references by the process.
This method can be achieved, but I do not know the internal implementation, there is to know, then you can tell me, thank you