Reputation: 71
I have a class called Time that contains objects whose instances have "hour" and "minute" parameters. In that class is a method called addMinutes which takes in an integer and adds that number of minutes to the "hour" and "minute" parameters of the object.
I have a constructor method in another class called Exercise that is supposed to make an object with the following parameters: String object called extype int object called min Time object called start Time object called end
I want the constructor to initialize start and then use the addMinutes method from Time to add that number of minutes and call that end. But when I use it, the start is updated with the end even if they are separate variables.
Here is the addMinutes method
public void addMinutes(int mins) {
this.mins += mins;
if (this.mins >= 60) {
addHours(this.mins / 60);
this.mins = this.mins % 60;
}
Here's an example of trying to use this and the output
public Exercise(String e, int m, Time s) {
extype = e;
min = m;
start = s;
System.out.println(start);
end = s;
System.out.println(start);
end.addMinutes(min);
System.out.print(s);
start = s;
System.out.println(start);
The output from running this with e = "Dancing", m = 90, and S = 15:45
Exercise e1 = new Exercise("Dancing",90,firsttime);
15:45
15:45
17:15 17:15
So when I use end.addMinutes(min) all the variables, start, end, and even s are updated to what I want to be the end time. Any insight on this?
Thanks.
Upvotes: 1
Views: 2053
Reputation: 311008
Why is Java updating multiple variables with the same value?
It isn't. It is updating a single object, and you have multiple variables that all refer to that object, so whichever variable you use to get a value inside the object, you always get the same value.
Upvotes: 0
Reputation: 12907
It is because Java uses references for its objects, not valules
Thus, when you do start = s
, it doesn't set start to the value of s, but set start to be the same object as s. And so on for the other assignement.
Then, when you call a method on any of these (namely : start, end, or s), you call this method on one and only one object, referenced by these three variables.
Upvotes: 1
Reputation: 1011
In java, variables that store objects are in fact storing "references" to objects. In other words, if you have the following:
Object obj;
cpy = obj;
Both obj
and copy
will point to the SAME object. If you update EITHER of them, BOTH will be affected.
Upvotes: 0
Reputation: 1218
This is because all objects in Java are implicetly by reference. If you say that end is equal to start, then they both refer to the same object, so updating the one results in updating the other.
What you can do is the following: let addMinutes() return a new Time object with the specified minutes and hours. Then don't state end=start
but do this:
Time end;
end = start.addMinutes(m);
this should work.
Good luck!
Upvotes: 0
Reputation: 13133
When you execute end = s;
, you are setting the object referenced by end
to the the same one as referenced by s
. Therefore, when you change the object, it is reflected in both references. If you want them to be different, they have to be set to different variables.
Give your class a "clone" method that creates a new object of the same type and sets all internal variables of the new object to those of the old one. Then clone instead of using '=', and you will have separate objects.
Upvotes: 0