[Java] Setting the variables in 2 objects equal

Started by
6 comments, last by Greenbird 12 years, 8 months ago
Hey. I've been searching for how to fix this problem of mine but I can't find anything.
In my main method, I have two objects that are instances of the same class called Robot. I would like all of the variables in one of the objects to equal all of the variables of the other object without setting each one individually. Since they are both instances of the same class, they have all of the same variables.
Simply putting Robot1 = Robot2; doesn't work because that just sets the addresses/paths to the data in memory equal to each other.
I found the equals(); method, but I think that's just for comparing the data of the objects instead of setting the data.

Is there some kind of trick to setting all of the variables equal or do I just have to do it the long way? Thank you for your help.
Advertisement
sure there are some hacks which can copy a obejct perfectly but just create a setData methode or so which takes a Robot object and copys all relevant data
Do you want a deep or shallow copy? E.g. if the Robot contained a List (or other container), should each Robot point at the same list or should they both get an independent copy of the list, including copies of all sub-objects?
I would like for each object to have their own independent copies of the variables, lists, etc. The two objects' variables will only be set equal to each other under certain circumstances. Otherwise, they will be changing their own variables independently of the other object. The Robot class won't have any subclasses, so each object having it's own sub-objects is irrelevant.
I want to try to avoid setting every variable equal to the one in the other object manually. There are a lot of variables and I don't want to have to change the code whenever I add a new variable or change an existing one.

I would like for each object to have their own independent copies of the variables, lists, etc. The two objects' variables will only be set equal to each other under certain circumstances. Otherwise, they will be changing their own variables independently of the other object. The Robot class won't have any subclasses, so each object having it's own sub-objects is irrelevant.
I want to try to avoid setting every variable equal to the one in the other object manually. There are a lot of variables and I don't want to have to change the code whenever I add a new variable or change an existing one.


Didn't read this whole thing, but it should give you an idea where to go:
http://javatechniques.com/blog/faster-deep-copies-of-java-objects/
From reading that tutorial, it seems like the clone(); method is used to create a cloned object that has all of the same variables as the first. I don't want to create a new object every cycle, though.

Here's a more specific breakdown of what I am trying to do:
*I create Robot1 and Robot2 that are both objects of the Robot class.
*I manually set certain variables from Robot2 equal to certain ones from Robot1, such as: Robot2.Speed = Robot1.Speed, etc
*I edit the all of the variables that belong to Robot2, including the ones I manually set from Robot1 and more.
*Now I want to set all of the variables from Robot1 to what they were changed to when I edited Robot2. I don't want to have to manually do them all because there are a lot more that were changed.

I do it in this roundabout way because I don't want Robot1 to be able to edit any of Robot2's variables without me specifically saying so (like how I make Robot1.Speed = Robot2.Speed). If Robot1 and Robot2 had the same path, then Robot1 would be able to set variables in Robot2 that I didn't want it to.
You can use Java reflection to set the values at runtime. Another option is to write it manually, and use an assert that the objects are in disjoint object graphs but are equal (again using Java reflection).

Correctly handling all corner cases with reflection can be tricky. Can you post your Robot class?

Hey. I've been searching for how to fix this problem of mine but I can't find anything.
In my main method, I have two objects that are instances of the same class called Robot. I would like all of the variables in one of the objects to equal all of the variables of the other object without setting each one individually. Since they are both instances of the same class, they have all of the same variables.
Simply putting Robot1 = Robot2; doesn't work because that just sets the addresses/paths to the data in memory equal to each other.
I found the equals(); method, but I think that's just for comparing the data of the objects instead of setting the data.

Is there some kind of trick to setting all of the variables equal or do I just have to do it the long way? Thank you for your help.


Hey Polar1080,

Perhaps you are already aware of this and this is what you meant by the long way but, I believe an easy way you could implement what you are talking about is to create a new method in the Robot class say, called RobotsEqual(). Then in this method create new variables set them equal to each robot and then set the new variables equal to each other. When you want to have the condition that the robots are equal to each other then just call this method. Perhaps I do no fully understand the problem so if my solution is incorrect please excuse me for my stupidity lol. To better illustrate what I mean here is some sample code excuse my syntax errors I havnt used java in a while.



setRobotsEqual(var 1, var 2)
{

var1 = this.Robot1.speed();
var2 = this.Robot2.speed();

var1 = var2;
}



This topic is closed to new replies.

Advertisement