public class MyDate {
private int day = 1;
private int month = 1;
private int year = 2000;
public MyDate(int day, int month, int year)
{
this.day = day;
this.month = month;
this.year = year;
}
public MyDate(MyDate date)
{
this.day = date.day;
this.month = date.month;
this.year = date.year;
}
/*
here why do we need to use Class name before the method name?
*/
public MyDate addDays(int moreDays)
{
// "this" is referring to which object and why?
MyDate newDate = new MyDate(this);
newDate.day = newDate.day + moreDays;
// Not Yet Implemented: wrap around code...
return newDate;
}
public String toString()
{
return "" + day + "-" + month + "-" + year;
}
}
public class MyDate { private int day = 1; private int month = 1;
Share
thiswill refer to the current object instance that you will create. Inside any java methods,thiswill always hold a reference to its object instance.An example –
The line that you were wondering about will point to the
newDateobject that is created here. The line –will use this constructor –
to create and return a new object, passing it a reference to the current object instance, so that it can copy its day, month and year values.