Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > java.lang.StackOverflowError

Reply
Thread Tools

java.lang.StackOverflowError

 
 
Asad Khan
Guest
Posts: n/a
 
      10-27-2003
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.



 
Reply With Quote
 
 
 
 
Christophe Vanfleteren
Guest
Posts: n/a
 
      10-27-2003
Asad Khan wrote:

> 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.

--
Regards,
Christophe Vanfleteren
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off




Advertisments