You may consider adding methods:
equals()
hash()
and implement the Comparable interface
int compareTo( Object other )
In addition, it is recommended to make it unchanged.
For example, if you use this class to check if something should happen:
class Remainder {
private String what;
private DateTime when;
public static Remainder remindMe( String what, DateTime when ) {
Reminder r = new Reminder();
r.what = what;
r.when = when;
}
public boolean isTimeAlready() {
//return DateTime.Builder.createTime().compareTo( this.when ) > 0;
// implemented somehow
return isCurrentTimeGreaterThan( this.when ); // assume it return true if current time is after "when"
}
}
If you use it as follows:
DateTime atSix = new DateTime( 18, 0, 0 );
Reminder reminder = Reminder.remindMe("Go for the milk", atSix );
And the hour changes (by mistake, of course)
atSix.setHour( 1 );
You cannot use the Reminder object if the variable whenis private, because this link is stored externally and has no control over it, so it becomes unreliable.
, . . Java, String, Integer , .
: Java, 180- Java.