I want to get previous value of variable (gmtHourPrev), so I do as following. After change time for testing purpose, I saw the value of gmtHourPrev always equal gmtHour and can’t get previous value of it. What am I doing wrong here ? Thanks
private int gmtHour;
private int gmtHourPrev;
public int getHour() {
return gmtHour;
}
public void setGmtHour(int gHour) {
this.gmtHourPrev = this.gmtHour;
this.gmtHour = gHour;
}
public int getGmtHourPrev() {
return gmtHourPrev;
}
public String getGmtHourInfo() {
gmtHour = Calendar.HOUR;
setGmtHour(gmtHour);
if (isOK() == true) {
return gmtHour;
}
else {
return getGmtHourPrev();
}
}
Look at this:
Aside from the fact that
Calendar.HOURprobably isn’t what you think it is (that’s a constant) you’re effectively setting the variable twice. The above code is equivalent to:That’s equivalent to:
You need to only set it once, e.g. just calling
Again though, that value almost certainly isn’t what you want it to be…
You probably want:
for some instance of
Calendar.Oh, and trying to return an
intfrom a method declared to returnStringisn’t a great idea either…