java.lang.StackOverflowError

Discussion in 'Java' started by Asad Khan, Oct 27, 2003.

  1. Asad Khan

    Asad Khan Guest

    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.
    Asad Khan, Oct 27, 2003
    #1
    1. Advertising

  2. 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
    Christophe Vanfleteren, Oct 27, 2003
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Maciek Zywno
    Replies:
    0
    Views:
    851
    Maciek Zywno
    Sep 3, 2003
  2. Winona
    Replies:
    0
    Views:
    452
    Winona
    Dec 22, 2003
  3. Will
    Replies:
    7
    Views:
    16,567
    Adam Jenkins
    Jan 14, 2004
  4. Thomas Jollans

    How to stop StackOverflowError

    Thomas Jollans, Jan 31, 2004, in forum: Java
    Replies:
    5
    Views:
    602
    Sudsy
    Jan 31, 2004
  5. Replies:
    14
    Views:
    5,290
    Oliver Wong
    Jan 26, 2007
Loading...

Share This Page