java.lang.StackOverflowError

A

Asad Khan

I have the following method inside I class,

public boolean equals(Object o) {
return (this.equals(o));
}

but when i call this method by "foo.equals(bum)" where foo and bum are some
objects, I get a java.lang.StackOverflowError??

I kinda know why its happening; I think it calls the same equals method and
keep going round and round. But, this is what I have to do, without renaming
the method, so what's the way around it?

Thanks.
 
C

Christophe Vanfleteren

Asad said:
I have the following method inside I class,

public boolean equals(Object o) {
return (this.equals(o));
}

but when i call this method by "foo.equals(bum)" where foo and bum are some
objects, I get a java.lang.StackOverflowError??

I kinda know why its happening; I think it calls the same equals method and
keep going round and round. But, this is what I have to do, without
renaming the method, so what's the way around it?

Thanks.

Ofcourse it calls the same equals method. What else would you expect?
But why do you think that this is what you have to do? Do you have some other
implementation of equals?

If you also have something like a

public boolean equals(SUBCLASS_OF_OBJECT o)

method in your class, you can call it by being more specific in the call you
make in your equals method by casting the argument:

public boolean equals(Object o) {
return (this.equals((SUBCLASS_OF_OBJECT)o));
}

But it is poor form to do things like this. You should only write one equals
method, with the correct signature.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top