Problem with "if" statement functionality
807591Apr 8 2008 — edited Apr 8 2008I have a program below:
//Start of the program
//Tester Class
public class EqualsTest
{
public static void main(String args[])
{
Sample s1 = new Sample(8);
Sample s2 = new Sample(8);
Sample s3 = new Sample(2);
System.out.println("s1.equals.s2 is "+s1.equals(s2));
System.out.println("s1.equals.s3 is "+s1.equals(s3));
}
}
//Class which is to be tested for equality
class Sample
{
private int num;
Sample(int x)
{
num = x;
}
public int getNumber()
{
return num;
}
public boolean equals(Object obj)
{
if ((obj instanceof Sample)&&(((Sample)obj).getNumber()==this.num))
{
return true;
}
}
}
//End of the program
The above code seems to be all right.
But , I get as output the following error:
EqualsTest.java:36: missing return statement
*}*
But if I append an else statement after the if statement,
the code works fine. I do not understand what is the problem. Please help immediately.