This is how I would clean up your code:
class Data {
private Calendar calendar;
Data(Calendar x) { calendar = x; }
public String whichIsEarlier(Data other) {
return (calendar.after(other.calendar)? this : other)
+ " jest wczesniej";
}
(I have elided toString
; I'm not getting into its issues for this answer).
Note:
- removed the
cale
instance variable - removed the two-argument constructor;
whichIsEarlier
accepts anotherData
, not anotherCalendar
;- its return value doesn't call
calendar.toString
but rather your own object's toString - and it does it implicitly (string concatenation does that).
And now the main method should go like
public static void main(String[] args) {
Calendar yy =Calendar.getInstance();
Calendar xx= Calendar.getInstance();
xx.set(2001, 10,10);
Data dt = new Data(yy);
Data xt = new Data(xx);
System.out.println(dt.whichIsEarlier(xt));
}