I have a piece of code I dont quite understand. Its causing a bug.
There’s a Calendar object and a method which writes this calendar object to a string, but increments the year first. however, even though there are 2 Calendar objects, they are both being rolled.
See the method that does the roll below
public static synchronized Calendar rollDatePlus1Year(Calendar currentDate){
Calendar rtn = currentDate;
rtn.roll(Calendar.YEAR, 1);
rtn.roll(Calendar.MINUTE, -1);
return rtn;
}
from this both “currentDate” and “rtn” are incremented by a year. This method should not change any values, but return a new represenation.
Any ideas?
Line
Calendar rtn = currentDate;assigns reference to object to other variable. From this pointrtnandcurrntDaterefer to the same object. If you want, the same place in memory. Therefore all changes done using one of these references are visible using the second one because are done on the same object.