Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Problem with "if" statement functionality

807591Apr 8 2008 — edited Apr 8 2008
I 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.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on May 6 2008
Added on Apr 8 2008
16 comments
177 views