I have a domain class x which have startdate and enddate among its attributes.so in my service i used this code to set the two values `
def date = new Date()
date.set(hourOfDay: 12, minute: 0, second: 0, year: 2012 , month: 0, date: 1)
x.startDate = date
date.set(hourOfDay: 12, minute: 0, second: 0, year: 12 , month: 11, date: 30)
x.endDate = date`
but the problem is it's giving the same value(month 1 year 2012) to both attributes eventhough i assigned diffrent value to them.
i tried using a debugger to see why this is happening and at first it assigns the proper value to startdate but when the value of date is changed it also changes the value of startdate (at line 4). the possible solution(workaround) is creating two date variables but i cant seem to understand why this is happening. can some one tell me why this is happening
You're right about the solution. You need separate date variables.
When you assign the date to the startDate or endDate properties you're setting a reference to the date variable. By referencing the same date variable in both properties any changes to the date variable are reflected in both properties.