We are about to switch to a new forum software. Until then we have removed the registration on this forum.
So let's say there's a "Car" class. Car car1=new Car(some_attributes); This line will declare a new Car and stores the object onto car1 Car car2=car1; This line will assign car2 an object whose memory at the exact location as car1. So this means when i change car1, car2 will also be affected.
But i want car1 and car2 to initially have the same attributes but then i can change them independently. I can do this by declaring a new Car all over again, reconstruct everything that's in car1. This is fine for simple classes but as the class fills up with functions and variables, it increasingly becomes tedious. So i'm wondering if you guys have a better solution to this?
Answers
http://docs.Oracle.com/javase/8/docs/api/java/lang/Object.html#clone--
https://forum.Processing.org/two/discussions/tagged?Tag=clone()
Did you understand @GoToLoop's answer?
Relevant previous post::
https://forum.processing.org/two/discussion/15319/copy-objects#latest
Kf
Yeah, better.
It also depends on when you need your copy. In your scenario, it sounds like you are creating both cars at the same time -- initializing two objects based on the same constructor arguments may be a bit more verbose, but if that is what you are really doing then code with two
new car
lines is easier to read and easier to maintain than creating one car, making a custom copy, then modifying the copy.