Java – calendar volume affects other calendars

I have a piece of code that I don't quite understand It caused a mistake

There is a calendar object and a method to write the calendar object to a string, but first increase the year However, even if there are two calendar objects, they are all scrolled

See the scroll method below

public static synchronized Calendar rollDatePlus1Year(Calendar currentDate){
    Calendar rtn = currentDate;
    rtn.roll(Calendar.YEAR,1);
    rtn.roll(Calendar.MINUTE,-1);
    return rtn;
}

Both "CurrentDate" and "RTN" have been added for one year This method should not change any value but return a new representation

Any ideas?

Solution

Line calendar RTN = CurrentDate; Assign references to objects to other variables From this point on, RTN and currntdate refer to the same object If you want, the same location in memory Therefore, all changes made with one reference are visible with the second reference because they are done on the same object

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>