class DogOwner {
Dog dog;
DogOwner(Dog dog) {
this.dog = dog;
}
}
class Dog {
int age;
Dog(int age) {
this.age = age;
}
}
DogOwner peter = new DogOwner(new Dog(2));
Dog max = peter.dog;
max.age = 3;
System.out.println(peter.dog.age); // 3
How can I retreive max from peter without max being a reference to the Dog owned by peter? In other words, I would like to be able to set max's age to 3 without peter's Dog being changed.
You either have to clone peter.dog, or create a new instance based on it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With