In comparing two objects.
class date
{
private int a,b;
public date(int a,int b)
{
this.a=a;
this.b=b;
}
public boolean equals(Object compared) {
if (this == compared) {
return true;
}
if (!(compared instanceof date)) {
return false;
}
if (this.a == compared.a &&this.b == compared.b) //Error - cannot find the variable a and b
{
return true;
}
return false;
}
public static void main(String args[])
{
date abc=new date(14,20);
date xyz=new date(14,21);
if(abc.equals(xyz))
{
System.out.println("Objects are equal");
}
else
{
System.out.println("Objects are not equal");
}
}}
In the above program i am comparing two objects. I have added a comment line(//)to show which line was giving error. Why is this error coming? I am simply giving the reference of xyz to compare. it should work. when I am adding this line the program is working.(ignore "simpledate" by that i meant "date"
What I know is we cannot change the type of object once we have created it. if we want to compare two objects we can simply do it by "if(abc.a==xyz.aa &&abc.b==xyz.b)" Why in above program do i have to change the type?